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 scope = current_scope if scope if self == scope.klass scope.clone else relation.merge!(scope) end else default_scoped end end
def default_extensions # :nodoc:
def default_extensions # :nodoc: if scope = scope_for_association || build_default_scope scope.extensions else [] end end
def default_scoped(scope = relation, all_queries: nil)
def default_scoped(scope = relation, all_queries: nil) build_default_scope(scope, all_queries: all_queries) || 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
{has_many}[rdoc-ref:Associations::ClassMethods#has_many] declarations:
\Named scopes can also have extensions, just as with
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
associations. If,
available to {has_many}[rdoc-ref:Associations::ClassMethods#has_many]
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
declaration. For instance, you can invoke Shirt.red.first, Shirt.red.count,
constructed by a {has_many}[rdoc-ref:Associations::ClassMethods#has_many]
which is composable with other scopes; it resembles the association object
Shirt.red is not an Array but an ActiveRecord::Relation,
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
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).
A \scope represents a narrowing of a database query, such as
{all}[rdoc-ref:Scoping::Named::ClassMethods#all] scope is returned instead.
If it returns +nil+ or +false+, an
object, which is composable with other scopes.
The method is intended to return an ActiveRecord::Relation
Adds a class method for retrieving and querying objects.
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 if method_defined_within?(name, Relation) raise ArgumentError, "You tried to define a scope named \"#{name}\" " \ "on the model \"#{self.name}\", but ActiveRecord::Relation already defined " \ "an instance method with the same name." end extension = Module.new(&block) if block if body.respond_to?(:to_proc) singleton_class.define_method(name) do |*args| scope = all._exec_scope(*args, &body) scope = scope.extending(extension) if extension scope end else singleton_class.define_method(name) do |*args| scope = body.call(*args) || all scope = scope.extending(extension) if extension scope end end singleton_class.send(:ruby2_keywords, name) generate_relation_method(name) end
def scope_for_association(scope = relation) # :nodoc:
def scope_for_association(scope = relation) # :nodoc: if current_scope&.empty_scope? scope else default_scoped(scope) end end
def singleton_method_added(name)
def singleton_method_added(name) generate_relation_method(name) if Kernel.respond_to?(name) && !ActiveRecord::Relation.method_defined?(name) end