如何在Rails 3的范围ActiveRecord关联? [英] How do you scope ActiveRecord associations in Rails 3?

查看:226
本文介绍了如何在Rails 3的范围ActiveRecord关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3的项目。使用Rails 3来阿雷尔和重用一个作用域再建的能力。我想知道如果有一种方法,当定义关系(如的has_many)使用范围。

我有权限列记录。我想建立一个default_scope这需要我的允许列考虑到这样的记录(即使是通过关系进入)进行过滤。

presently,在Rails 3中,default_scope(包括补丁我发现)不提供通过一个进程(这是我需要的后期变量绑定)的可行手段。是否有可能定义一个的has_many成一个命名范围,可以通过?

重用一个名为范围会是什么样子的想法:

  Orders.scope:my_orders,拉姆达{其中(:user_ID的=> User.current_user.id)}
的has_many:订单:范围=> Orders.my_orders
 

或暗示的编码名为范围的关系将是这样的:

 的has_many:订单:范围=>拉姆达{其中(:user_ID的=> User.current_user.id)}
 

我只是尝试应用default_scope与后期绑定。我将preFER使用阿雷尔方法(如果有的话),但将使用任何可行的选项。

由于我这里指的是当前用户,我可以不依赖于没有在最后一刻被评估条件,如:

 的has_many:订单:条件=> [user_ID的=?,User.current_user.id]
 

解决方案

我建议你看一看的命名范围都死了

作者解释有阿雷尔多么强大:)

我希望能对大家有所帮助。

编辑#2014年3月1日

由于一些国家的意见,不同的是,现在的个人喜好问题。

不过,我还是个人建议,以避免暴露阿雷尔的范围,上层(该控制器或其他任何直接访问模式),而且这样做会要求:

  1. 创建一个范围,将其暴露在直通模型的方法。该方法将是一个你接触到的控制器;
  2. 如果你从来没有暴露你的模型,你的控制器(让你有一些在它们之上的服务层),那么你的罚款。反腐败层的的为您服务,它可以访问你的模型的范围,而不用担心太多有关如何作用域实现的。

I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many").

I have records which have permission columns. I would like to build a default_scope that takes my permission columns into consideration so that records (even those accessed through a relationship) are filtered.

Presently, in Rails 3, default_scope (including patches I've found) don't provide a workable means of passing a proc (which I need for late variable binding). Is it possible to define a has_many into which a named scope can be passed?

The idea of reusing a named scope would look like:

Orders.scope :my_orders, lambda{where(:user_id => User.current_user.id)}
has_many :orders, :scope => Orders.my_orders

Or implicitly coding that named scope in the relationship would look like:

has_many :orders, :scope => lambda{where(:user_id => User.current_user.id)}

I'm simply trying to apply default_scope with late binding. I would prefer to use an Arel approach (if there is one), but would use any workable option.

Since I am referring to the current user, I cannot rely on conditions that aren't evaluated at the last possible moment, such as:

has_many :orders, :conditions => ["user_id = ?", User.current_user.id]

解决方案

I suggest you take a look at "Named scopes are dead"

The author explains there how powerful Arel is :)

I hope it'll help.

EDIT #1 March 2014

As some comments state, the difference is now a matter of personal taste.

However, I still personally recommend to avoid exposing Arel's scope to an upper layer (being a controller or anything else that access the models directly), and doing so would require:

  1. Create a scope, and expose it thru a method in your model. That method would be the one you expose to the controller;
  2. If you never expose your models to your controllers (so you have some kind of service layer on top of them), then you're fine. The anti-corruption layer is your service and it can access your model's scope without worrying too much about how scopes are implemented.

这篇关于如何在Rails 3的范围ActiveRecord关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆