module ActiveRecord::NamedScope::ClassMethods
def named_scope(name, options = {}, &block)
expected_options = { :conditions => { :colored => 'red' } }
end
}
{ :conditions => { :color => color } }
named_scope :colored, lambda { |color|
class Shirt < ActiveRecord::Base
proxy_options method on the proxy itself.
For testing complex named \scopes, you can examine the scoping options using the
end
end
end
'red_shirts'
def dom_id
named_scope :red, :conditions => {:color => 'red'} do
class Shirt < ActiveRecord::Base
Named \scopes can also have extensions, just as with has_many declarations:
In this example, Shirt.colored('puce') finds all puce shirts.
end
}
{ :conditions => { :color => color } }
named_scope :colored, lambda { |color|
class Shirt < ActiveRecord::Base
Named \scopes can also be procedural:
only shirts.
then elton.shirts.red.dry_clean_only will return all of Elton's red, dry clean
end
has_many :shirts
class Person < ActiveRecord::Base
has_many associations. If,
All \scopes are available as class methods on the ActiveRecord::Base descendant upon which the \scopes were defined. But they are also available to
for which these criteria obtain. Similarly with Shirt.red.dry_clean_only.average(:thread_count).
Nested finds and calculations also work with these compositions: Shirt.red.dry_clean_only.count returns the number of garments
These named \scopes are composable. For instance, Shirt.red.dry_clean_only will produce all shirts that are both red and dry clean only.
Shirt.red.first, and Shirt.red.inject(memo, &block) all behave as if Shirt.red really was an Array.
as with the association objects, named \scopes act like an Array, implementing Enumerable; Shirt.red.each(&block),
Shirt.red.find(:all, :conditions => {:size => 'small'}). Also, just
constructed by a has_many declaration. For instance, you can invoke Shirt.red.find(:first), Shirt.red.count,
Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object
in effect, represents the query Shirt.find(:all, :conditions => {:color => 'red'}).
The above calls to named_scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
end
named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
named_scope :red, :conditions => {:color => 'red'}
class Shirt < ActiveRecord::Base
such as :conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions.
Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
def named_scope(name, options = {}, &block) name = name.to_sym scopes[name] = lambda do |parent_scope, *args| Scope.new(parent_scope, case options when Hash options when Proc if self.model_name != parent_scope.model_name options.bind(parent_scope).call(*args) else options.call(*args) end end, &block) end singleton_class.send :define_method, name do |*args| scopes[name].call(self, *args) end end
def scoped(scope, &block)
def scoped(scope, &block) Scope.new(self, scope, &block) end
def scopes
def scopes read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {}) end