Is it possible to add filter on an HABTM associated model's field ? #1342

Val opened this Issue Oct 1, 2012 · 8 comments


None yet
4 participants

Val commented Oct 1, 2012

I'm trying to override list view pretty_value on associated model to provide a quick link to associated model's list using filter to show only associated records.

It works well for has_many associations but not for HABTM ones.

To see it try this gist :

Author 1-N Book works
Book N-N Collection doesn't : if you try to add a book filter in collection list or a collection filter in book list there is no selector option ("contains", "is exactly", etc.)

Any tips or documentation pointer ?


bbenezech commented Oct 2, 2012

No, it's not possible at the moment.
Only belongs_to, sorry.

Val commented Oct 2, 2012

Ok, thanks.

May be I could help you providing this feature.
Is it a client-side problem, a server side one or the both ? Do you have any plan for it or any pointer in sources ?


Val commented Oct 2, 2012

Could you try this hack :

It's working for me but I'm not sure to follow your code style... Hope it helps.

Val commented Oct 4, 2012

Note: lines 42-44 of ra.filter-box.js were ended with a non-printable space character and I did remove them, that's why they appear on diff.


mshibuya commented Oct 25, 2012

Your work looks nice, but some more jobs still need to be done.

  1. It comes with no tests.
  2. Forcing include of has_and_belongs_to_many association may produce massive amount of DB access. Instead of it, we'll need some kind of a configuration option to control eager-loading of has_many/has_and_belongs_to_many association.

How's your opinion on them?

Val commented Oct 25, 2012

@mshibuya : Thanks for taking time to review the patch.

I agree with you, it needs tests and lacks some kind of configuration option and perhaps a better rails_admin DSL inclusion too.

I'll try to refactor my code.

I need same functionality for has_many :thorugh association may be it is possible to implement it too ? and i did not find any filter code for simple has_many association (without through) , I can try to implement it I think you code can help me. :)

mshibuya closed this in b92a4d1 Sep 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment