开发者

Rails - Conditions Include Multiple Items In An Array

p = Person.find_by_id(1, :include => :bags, :conditions => ['bag.id in (?), [3,4])

I would like to know how I could m开发者_JS百科ake sure this query will only be valid if both 'items.id' '3' & '4' are present rather than '3' or/and '4'.

Thanks


You need to manually construct your 2 inner joins to the bags_persons table:

Person.find_by_id(1, :joins => "INNER JOIN bags_persons bp1 ON bp1.person_id=persons.id INNER JOIN bags_persons bp2 ON bp2.person_id=persons.id", :conditions => "bp1.bag_id=3 AND bp2.bag_id=4")

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜