instance method scoped

Ruby on Rails 3.0.20

Since v2.3.18 Last seen in v3.1.12

Available in: v2.3.18 v3.0.20 v3.1.12

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(: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

options opt = nil
Source
# File activerecord/lib/active_record/named_scope.rb, line 28
      def scoped(options = nil)
        if options
          scoped.apply_finder_options(options)
        else
          current_scoped_methods ? relation.merge(current_scoped_methods) : relation.clone
        end
      end

Defined in activerecord/lib/active_record/named_scope.rb line 28 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::NamedScope::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close