相关疑难解决方法(0)

想在Rails 3中找到没有相关记录的记录

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

class Person
   has_many :friends
end

class Friend
   belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)

让所有在ARel和/或meta_where中没有朋友的人最简洁的方法是什么?

然后是一个has_many:通过版本

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
Run Code Online (Sandbox Code Playgroud)

我真的不想使用counter_cache - 而且我从我读过的内容中看起来并不适用于has_many:通过

我不想拉出所有的person.friends记录并在Ruby中循环它们 - 我希望有一个可以与meta_search gem一起使用的查询/范围

我不介意查询的性能成本

离实际的SQL越远越好......

ruby-on-rails arel meta-where

171
推荐指数
6
解决办法
6万
查看次数

Rails/SQL:查找没有孩子或所有孩子都患有某种疾病的父母

我需要找到没有孩子或所有孩子都患有疾病(状态 = 1)的父母。

class Parent
  has_many :children
end

class Child
  enum status: [ :confirmed, :not_confirmed ]
  belongs_to :parent
end
Run Code Online (Sandbox Code Playgroud)

我知道第一部分,就是寻找没有孩子的父母。

Parent.joins(:children).where('count(children) = 0')
Run Code Online (Sandbox Code Playgroud)

铁轨回答。

ruby sql postgresql activerecord ruby-on-rails

3
推荐指数
1
解决办法
1742
查看次数

标签 统计

ruby-on-rails ×2

activerecord ×1

arel ×1

meta-where ×1

postgresql ×1

ruby ×1

sql ×1