Rails 3 many to many query condition

前端 未结 3 572
名媛妹妹
名媛妹妹 2021-01-22 20:36

I\'m trying to do a simple Post/Tags relation in rails 3. Everything working fine except when I want to query the Posts which are related to several tags. Basically I\'d like to

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 21:33

    I'm stuck on the same problem. The squeel docuemtnation suggests this is possible. In the Compound Conditions section he lists three ways to do something similar.

    Given

    names = ['Ernie%', 'Joe%', 'Mary%']
    

    Then you can do

    Person.where('name LIKE ? OR name LIKE ? OR name LIKE ?', *names)
    

    or

    Person.where((['name LIKE ?'] * names.size).join(' OR '), *names)
    

    or

    Person.where{name.like_any names}
    

    The documentation implies we can use AND instead of OR or like_all as opposed to like_any.

    However I can't seem to get it to work for a habtm relationship. It keeps giving me undefined method 'call' for an instance of ActiveRecord.

提交回复
热议问题