Rails 2.3.x - how does this ActiveRecord scope work?

非 Y 不嫁゛ 提交于 2019-12-23 14:23:13

问题


There's a named_scope in a project I'm working on that looks like the following:

 # default product scope only lists available and non-deleted products
  ::Product.named_scope :active,      lambda { |*args|
    Product.not_deleted.available(args.first).scope(:find)
  }

The initial named_scope makes sense. The confusing part here is how .scope(:find) works. This is clearly calling another named scope (not_deleted), and applying .scope(:find) afterwards. What/how does .scope(:find) work here?


回答1:


A quick answer

Product.not_deleted.available(args.first)

is a named-scope itself, formed by combining both named scopes.

scope(:find) gets the conditions for a named-scope (or combination of scopes), which you can in turn use to create a new named-scope.

So by example:

named_scope :active,      :conditions => 'active  = true' 
named_scope :not_deleted, :conditions => 'deleted = false'

then you write

named_scope :active_and_not_deleted, :conditions => 'active = true and deleted = false'

or, you could write

named_scope :active_and_not_deleted, lambda { self.active.not_deleted.scope(:find) }

which is identical. I hope that makes it clear.

Note that this has become simpler (cleaner) in rails 3, you would just write

scope :active_and_not_deleted, active.not_deleted



回答2:


Scope is a method on ActiveRecord::Base that returns the current scope for the method passed in (what would actually be used to build the query if you were to run it at this moment).

# Retrieve the scope for the given method and optional key.
def scope(method, key = nil) #:nodoc:
  if current_scoped_methods && (scope = current_scoped_methods[method])
    key ? scope[key] : scope
  end
end

So in your example, the lambda returns the scope for a Product.find call after merging all the other named scopes.

I have a named_scope:

named_scope :active, {:conditions => {:active => true}}

In my console output, Object.active.scope(:find) returns:

{:conditions => {:active => true}}


来源:https://stackoverflow.com/questions/4764065/rails-2-3-x-how-does-this-activerecord-scope-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!