module ActiveRecord::Scoping::Named::ClassMethods
def all
You can define a scope that applies to all finders using
fruits = fruits.limit(10) if limited?
fruits = fruits.where(color: 'red') if options[:red_only]
fruits = Fruit.all
posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
posts.size # Fires "select count(*) from posts" and returns the count
posts = Post.all
Returns an ActiveRecord::Relation scope object.
def all if current_scope current_scope.clone else default_scoped end end
def default_scoped # :nodoc:
def default_scoped # :nodoc: relation.merge(build_default_scope) end
def scope(name, body, &block)
Article.published.featured.latest_article
We are able to call the methods like this:
end
end
pluck(:title)
def self.titles
end
order('published_at desc').first
def self.latest_article
scope :featured, -> { where(featured: true) }
scope :published, -> { where(published: true) }
class Article < ActiveRecord::Base
on scopes. Assuming the following setup:
\Class methods on your model are automatically available
Article.published.create.published # => true
Article.published.new.published # => true
end
scope :published, -> { where(published: true) }
class Article < ActiveRecord::Base
Scopes can also be used while creating/building a record.
end
end
end
'red_shirts'
def dom_id
scope :red, -> { where(color: 'red') } do
class Shirt < ActiveRecord::Base
declarations:
\Named scopes can also have extensions, just as with +has_many+
Elton's red, dry clean only shirts.
then elton.shirts.red.dry_clean_only will return all of
end
has_many :shirts
class Person < ActiveRecord::Base
available to +has_many+ associations. If,
descendant upon which the \scopes were defined. But they are also
All scopes are available as class methods on the ActiveRecord::Base
Similarly with Shirt.red.dry_clean_only.average(:thread_count).
returns the number of garments for which these criteria obtain.
with these compositions: Shirt.red.dry_clean_only.count
both red and dry clean only. Nested finds and calculations also work
Shirt.red.dry_clean_only will produce all shirts that are
These named \scopes are composable. For instance,
Shirt.red really was an Array.
and Shirt.red.inject(memo, &block) all behave as if
Enumerable; Shirt.red.each(&block), Shirt.red.first,
association objects, named \scopes act like an Array, implementing
Shirt.red.where(size: 'small'). Also, just as with the
Shirt.red.first, Shirt.red.count,
constructed by a +has_many+ declaration. For instance, you can invoke
Shirt.red is not an Array; it resembles the association object
Unlike Shirt.find(...), however, the object returned by
end
end
where(color: 'red')
def self.red
class Shirt < ActiveRecord::Base
class method:
Note that this is simply 'syntactic sugar' for defining an actual
time it is called.
with +scope+. This ensures that the scope is re-evaluated each
You should always pass a callable object to the scopes defined
represents the query Shirt.where(color: 'red').
Shirt.dry_clean_only. Shirt.red, in effect,
The above calls to +scope+ define class methods Shirt.red and
end
scope :dry_clean_only, -> { joins(:washing_instructions).where('washing_instructions.dry_clean_only = ?', true) }
scope :red, -> { where(color: 'red') }
class Shirt < ActiveRecord::Base
where(color: :red).select('shirts.*').includes(:washing_instructions).
represents a narrowing of a database query, such as
Adds a class method for retrieving and querying objects. A \scope
def scope(name, body, &block) unless body.respond_to?(:call) raise ArgumentError, 'The scope body needs to be callable.' end if dangerous_class_method?(name) raise ArgumentError, "You tried to define a scope named \"#{name}\" " \ "on the model \"#{self.name}\", but Active Record already defined " \ "a class method with the same name." end extension = Module.new(&block) if block singleton_class.send(:define_method, name) do |*args| scope = all.scoping { body.call(*args) } scope = scope.extending(extension) if extension scope || all end end
def scope_attributes # :nodoc:
an AR instance for the particular class this is called on.
Collects attributes from scopes that should be applied when creating
def scope_attributes # :nodoc: all.scope_for_create end
def scope_attributes? # :nodoc:
Are there default attributes associated with this scope?
def scope_attributes? # :nodoc: current_scope || default_scopes.any? end