我有这个查询。我想找一个没有地址但有地址的人的房子。房子属于地址,而人有许多地址。房子属于一个人,一个人有很多房子。
模型名称为House
,Person
和Address
。
House.joins(:person).where('houses.address.id = ? and persons.address_id = ?', 'nil', 'not nil')
上面的查询错误,但可能是我想要的。我该如何构建它?我的SQL不是很好。
答案 0 :(得分:1)
试试这个:
House.joins(:persons).where(address: nil).where.not(persons: {address: nil})