考虑一个简单的关联......
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越远越好......
我正在尝试做一些我认为很简单的事情,但似乎并不是这样.
我有一个有很多职位空缺的项目模型.
class Project < ActiveRecord::Base
has_many :vacancies, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
我想得到所有至少有1个空缺的项目.我试过这样的事情:
Project.joins(:vacancies).where('count(vacancies) > 0')
Run Code Online (Sandbox Code Playgroud)
但它说
SQLite3::SQLException: no such column: vacancies: SELECT "projects".* FROM "projects" INNER JOIN "vacancies" ON "vacancies"."project_id" = "projects"."id" WHERE ("projects"."deleted_at" IS NULL) AND (count(vacancies) > 0).