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
精彩评论