p = Person.find_by_id(1, :include => :bags, :conditions => ['bag.id in (?), [3,4])
我想知道如何确保此查询仅在'items.id''3'和& '4'存在而不是'3'或/和'4'。
由于
答案 0 :(得分:1)
您需要手动构建bag_persons表的2个内部联接:
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")
希望这有帮助