我有这个查询。我想找一个没有地址但有地址的人住的房子。房子属于一个地址,而一个人有很多地址。房子属于一个人,而一个人有很多房子。
模型名称为House
、Person
、Address
。
House.joins(:person).where('houses.address.id = ? and persons.address_id = ?', 'nil', 'not nil')
上面的查询是错误的,但可能是我想要的。我怎么构造它?我的SQL不是很好。
试试这个:
House.joins(:persons).where(address: nil).where.not(persons: {address: nil})