考虑一个简单的关联...
class Person
has_many :friends
end
class Friend
belongs_to :person
end
在Arel和/或meta_where中,让所有没有朋友的人最干净的方法是什么?
然后has_many:through版本呢?
class Person
has_many :contacts
has_many :friends, :through => :contacts, :uniq => true
end
class Friend
has_many :contacts
has_many :people, :through => :contacts, :uniq => true
end
class Contact
belongs_to :friend
belongs_to :person
end
我真的不想使用counter_cache-从我所读的内容来看,它不适用于has_many:through
我不想拉所有的person.friends记录并在Ruby中遍历它们-我想要一个可以与meta_search gem一起使用的查询/范围
我不在乎查询的性能成本
而且离实际SQL越远越好...
这仍然非常接近SQL,但是在第一种情况下,应该使每个人都没有朋友:
Person.where('id NOT IN (SELECT DISTINCT(person_id) FROM friends)')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句