首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我想在Rails中查找没有关联记录的记录

我想在Rails中查找没有关联记录的记录
EN

Stack Overflow用户
提问于 2011-03-16 07:47:10
回答 8查看 80.2K关注 0票数 205

考虑一个简单的关联..。

代码语言:javascript
复制
class Person
   has_many :friends
end

class Friend
   belongs_to :person
end

要让所有在ARel和/或meta_where上没有朋友的人都加入进来,最干净的方法是什么?

那么has_many :直通版本呢?

代码语言:javascript
复制
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:

我不想提取所有的person.friends记录,然后在Ruby语言中遍历它们--我希望有一个可以在meta_search gem中使用的查询/范围

我不关心查询的性能成本

离实际的SQL越远越好。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5319400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档