我需要创建一个查询,根据会议
返回所有买家我有2个模特
class Buyer < ActiveRecord::Base
has_many :meetings
end
class Meeting < ActiveRecord::Base
belongs_to :buyer
end
Buyers(id, name)
Meetings(id, buyer_id, value)
现在我需要创建一个查询,它将返回所有没有会议值为false的买家
我的意思是记录:
Buyers:
1, Toms
2, Ingus
Meetings
1, 1, true
2, 1, false
3, 2, true
4, 2, true
只应选择Ingus
编辑:
我猜问题看起来像这样
select * from Buyers where id in (select buyer_id from Meetings where value = false group by buyer_id having count(*) = 0)
但是如何进入Rails这么漂亮呢?
答案 0 :(得分:0)
你能试试吗?
Buyer.joins(:meetings).where("meetings.value = ?", false).distinct