instance method scoped

Ruby on Rails 3.2.22.5

Since v3.2.22.5 Last seen in v3.2.22.5

Signature

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(:color => '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

options opt = nil
Source
# File activerecord/lib/active_record/scoping/named.rb, line 30
        def scoped(options = nil)
          if options
            scoped.apply_finder_options(options)
          else
            if current_scope
              current_scope.clone
            else
              scope = relation
              scope.default_scoped = true
              scope
            end
          end
        end

Defined in activerecord/lib/active_record/scoping/named.rb line 30 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Scoping::Named::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close