问题描述
问候,
我有一个应用程序,其中公司
和用户
需要通过一个 CompanyMembership 模型,其中包含有关成员(特别是额外的信息,用户是否是本公司的管理,通过一个布尔值管理
)。简化版本的code:
I have an application where Companies
and Users
need to belong to each other through a CompanyMembership
model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a boolean value admin
). A simple version of the code:
class CompanyMembership < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
class Company < ActiveRecord::Base
has_many :company_memberships
has_many :users, :through => :company_memberships
end
class User < ActiveRecord::Base
has_many :company_memberships
has_many :companies, :through => :company_memberships
end
当然,这简化了获得公司全体成员通过 company.users.all
等。不过,我试图让一个公司谁是公司的管理员所有用户的列表(也检查用户是否是特定公司的管理)。我的第一个解决方案是在以下 company.rb
:
Of course, this makes it simple to get all the members of a company via company.users.all
, et al. However, I am trying to get a list of all Users in a Company who are admins of that Company (and also to test whether a user is an admin of a given company). My first solution was the following in company.rb
:
def admins
company_memberships.where(:admin => true).collect do |membership|
membership.user
end
end
def is_admin?(user)
admins.include? user
end
虽然这个作品,有什么感觉效率不高吧(它遍历每个成员,每次执行SQL,对不对?抑或是关系更聪明?),我不知道是否有更好的方法来进行此事(也许使用范围或花哨的新关联
对象Rails 3的用途?)。
While this works, something feels inefficient about it (it's iterating over each membership, executing SQL each time, right? Or is Relation smarter than that?), and I'm not sure if there's a better way to go about this (perhaps using scopes or the fancy new Relation
objects that Rails 3 uses?).
这是最好的方法的任何建议PROCEDE(preferably使用Rails 3最佳做法)将大大AP preciated!
Any advice on the best way to procede (preferably using Rails 3 best practices) would be greatly appreciated!
推荐答案
我相信我会对此错误的方式,对 company_memberships
指定,而不是条件用户
,这是我真正想要的(对用户
的列表,而不是一个列表 CompanyMemberships
)。我想,我一直在寻找解决的办法是:
I believe I was going about this the wrong way, specifying conditions on company_memberships
instead of users
, which was what I actually wanted (a list of Users
, not a list of CompanyMemberships
). The solution I think I was looking for is:
users.where(:company_memberships => {:admin => true})
生成以下SQL(为公司的1号):
which generates the following SQL (for company with ID of 1):
SELECT "users".* FROM "users"
INNER JOIN "company_memberships"
ON "users".id = "company_memberships".user_id
WHERE (("company_memberships".company_id = 1))
AND ("company_memberships"."admin" = 't')
我不知道但如果我需要它,但包括()
方法将执行预先加载,以保持下来的SQL查询的数目,如果必要的:
I'm not sure yet if I'll need it, but the includes()
method will perform eager loading to keep down the number of SQL queries if necessary:
活动记录,您可以指定 推动所有有关联 将要被加载。这是可能的 通过指定的包括
方法 在 Model.find
通话。随着包括, 活动记录,确保所有的 指定的协会是装 使用的最小可能数 queries.queries。
(我仍然开放给任何人谁认为这是不是最好的/最有效的/正确的方式来进行此事的任何建议。)
(I'm still open to any suggestions from anyone who thinks this isn't the best/most effective/right way to go about this.)
这篇关于在的has_many过滤子对象:通过Rails 3的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!