Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

named_scope :all is missing

#1959

The documentation for ActiveRecord::NamedScope states:

All subclasses of ActiveRecord::Base have two named scopes: * all - which is similar to a find(:all) query, and * scoped - which allows for the creation of anonymous scopes, on the fly

However, there is no named_scope named 'all'.

In script/console:


>> User.all.class
=> Array
>> User.scoped(:conditions => {}).class
=> ActiveRecord::NamedScope::Scope

I was able to correct the issue with the following addition to activerecord/lib/active_record/named_scope.rb:


     def self.included(base)
       base.class_eval do
         extend ClassMethods
         named_scope :scoped, lambda { |scope| scope }
+        named_scope :all, lambda { { :conditions => {} } }
       end
     end

I'm not sure that's the right fix, but it seems to work well for me. But I'm guessing that ActiveRecord::Base#all will also need to be removed.

Reported by Craig Buchek · February 13th, 2009 @ 04:54 AM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Craig Buchek
    Craig Buchek

    Duh, don't need the lambda in the definition of the named_scope. This should suffice:

    
    named_scope :all, :conditions => {}
    

    February 13th, 2009 @ 05:44 AM

  2. Zack Hobson
    Zack Hobson

    I like this idea as well, but this would change the current semantics of ActiveRecord::Base.all. Also, ActiveRecord has default_scope, which may cause confusion about whether you're supposed to redefine all or use default_scope when you want uniform behaviour from your finders.

    Perhaps the documentation should be updated instead to indicate that all is not a named_scope, and also provide a pointer to default_scope before users can get any clever ideas.

    On reflection, I don't think the functionality needs to change, just the documentation. It's an appealing idea (evidently whoever wrote the docs thought so too) but there's no big win to be achieved by changing the existing semantics.

    February 13th, 2009 @ 07:12 PM

  3. Zack Hobson
    Zack Hobson
    • Tag changed from activerecord, edge, named_scope to activerecord, edge, named_scope, patch

    Here's a patch that updates the documentation so it no longer indicates that all is a named_scope. I also added a (helpful?) pointer to default_scope.

    February 13th, 2009 @ 07:28 PM

  4. Repository
    Repository
    • State changed from new to committed

    (from [ff894b55dd787af1d6284567f2a898090aae64ca]) Update rdoc: all is not a named_scope [#1959 named_scope :all is missing state:committed]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    February 27th, 2009 @ 01:43 PM