2

I have a model setup like this:

  • Sale belongs to Product
  • Product belongs to Varietal

Now I want to make a select drop down filter for varietal on sales. The only way I've managed to do this is by name.

filter :product_varietal_name,
  as: :select,
  label: 'Varietal name',
  collection: Varietal.names_for_select

But I want to be able to serach by varietal_id since I have an index on that column, it should be much faster. I seem to be unable to do so, however.

filter :product_varietal       # undefined method `product_varietal_eq'
filter :product_varietal_id    # undefined method `product_varietal_eq'
filter :product_varietal_id_eq # undefined method `product_varietal_id_eq_eq'
filter :product_varietal_eq    # undefined method `product_varietal_eq_eq'

There is clearly some magic here around the _id at the end of the filter name. And this magic is not doing what I want.

The only case this does work is when the association is on the Model being viewed. So Sale belongs to Product, and filter :product works as you would expect.

So how can I make a drop down select box filter that searches on sale.product.varietal_id?

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • Since it works on a direct association you could try `has_one :varietal, through: :product` inside `Sale` and then `filter :varietal` – mdesantis Nov 25 '13 at 19:54
  • @mdesantis That might work, but I'd rather not change the data model just to make this searchable. I'm sure search can do what I want, I'm just not finding the right magic syntax. – Alex Wayne Nov 25 '13 at 19:57
  • 1
    I'd suggest trying the latest code on GitHub to see if `filter :product_varietal_id` works there – seanlinsley Nov 28 '13 at 22:30

0 Answers0