This project is archived and is in readonly mode.

#1959 ✓committed
Craig Buchek

named_scope :all is missing

Reported by Craig Buchek | February 13th, 2009 @ 04:54 AM | in 2.x

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.

Comments and changes to this ticket

  • Craig Buchek

    Craig Buchek February 13th, 2009 @ 05:44 AM

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

    
    named_scope :all, :conditions => {}
    
  • Zack Hobson

    Zack Hobson February 13th, 2009 @ 07:12 PM

    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.

  • Zack Hobson

    Zack Hobson February 13th, 2009 @ 07:28 PM

    • 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.

  • Repository

    Repository February 27th, 2009 @ 01:43 PM

    • State changed from “new” to “committed”

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

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages