instance method
scoped
Ruby on Rails 3.1.12
Since v2.3.18 Last seen in v3.1.12Signature
scoped(options = nil)
Returns an anonymous \scope.
posts = Post.scoped posts.size # Fires "select count(*) from posts" and returns the count posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects fruits = Fruit.scoped fruits = fruits.where(:colour => 'red') if options[:red_only] fruits = fruits.limit(10) if limited?
Anonymous \scopes tend to be useful when procedurally generating complex queries, where passing intermediate values (\scopes) around as first-class objects is convenient.
You can define a \scope that applies to all finders using ActiveRecord::Base.default_scope.
Parameters
-
optionsopt = nil
Source
# File activerecord/lib/active_record/named_scope.rb, line 29
def scoped(options = nil)
if options
scoped.apply_finder_options(options)
else
if current_scope
current_scope.clone
else
scope = relation.clone
scope.default_scoped = true
scope
end
end
end
Defined in activerecord/lib/active_record/named_scope.rb line 29
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::NamedScope::ClassMethods