开发者

Rails - Conditions Include Multiple Items In An Array

开发者 https://www.devze.com 2023-01-19 22:17 出处:网络
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\' \
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

精彩评论

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

关注公众号