在的has

编程入门 行业动态 更新时间:2024-10-28 00:25:10
本文介绍了在的has_many过滤子对象:通过Rails 3的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问候,

我有一个应用程序,其中公司和用户需要通过一个 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"pany_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。 回报率指南:ActiveRecord的查询

Active Record lets you specify in advance all the associations that are going to be loaded. This is possible by specifying the includes method of the Model.find call. With includes, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.queries. RoR Guides: ActiveRecord Querying

(我仍然开放给任何人谁认为这是不是最好的/最有效的/正确的方式来进行此事的任何建议。)

(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

本文发布于:2023-10-18 06:02:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!