views:

37

answers:

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

I would like to know how I could make sure this query will only be valid if both 'items.id' '3' & '4' are present rather than '3' or/and '4'.

Thanks

A: 

Hi

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

Jonathon Horsman