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 does not respond to array method find_all

#730

When calling find_all on a named_scope the method is not delegated to proxy_found (the actual collection) as expected. Instead is is delegated to the proxy_scope which results in a NoMethodError.

Calling .to_a on the scope explicitly works around this problem.

Example:


Topic.base.find_all(&:approved) # => NoMethodError: undefined method `find_all' for #<Class:0x19a0fb4>

Topic.base.to_a.find_all(&:approved) # => [#<Reply:0x179e720>#<Topic:0x179e388>#<Reply:0x179e20c>]

Test:

rake test_mysql TEST=test/cases/named_scope_test.rb

=> 1) Failure:

=> test_should_delegate_all_array_methods_not_defined_on_proxy_scope(NamedScopeTest)

=> Named Scope does not respond to find_all.


class NamedScopeTest < ActiveRecord::TestCase
  def test_should_delegate_all_array_methods_not_defined_on_proxy_scope
    [].methods.each do |m|
      begin # testing all methods that do not get delegated, if proxy_scope does not handle it, then it should have been delegated
        Topic.base.send m if m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last|empty?)/
      rescue Exception => e
        assert !$!.is_a?(NoMethodError), "Named Scope does not respond to #{m}"
      end
    end
  end
end

Possible Hackish Fix (probably better to update the regex to let find_all get delegated):


class Scope
      attr_reader :proxy_scope, :proxy_options

      [].methods.each do |m|
        unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last|empty?)/
          delegate m, :to => :proxy_found
        end
      end

      delegate :scopes, :with_scope, :to => :proxy_scope
+     delegate :find_all, :to => :proxy_found

Reported by Brad Sumersford · July 31st, 2008 @ 03:36 PM

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

Activity

  1. Brad Sumersford
    Brad Sumersford

    Not sure why it puts a semicolon after the & in a highlighted source code section,

    July 30th, 2008 @ 11:15 PM

  2. Brad Sumersford
    Brad Sumersford

    Just using Topic.base.select(&:approved) would be a better workaround, but I still like my find_all :)

    August 11th, 2008 @ 07:58 PM

  3. Ryan Bates
  4. Ryan Bates
    Ryan Bates
    • Tag set to activerecord, named_scope, patch

    Here's an attached patch which just modifies the regular expression.

    Speaking of which, I don't care for that regular expression. It has some bad side effects like this, and it just looks ugly. Anyone have an idea for a refactoring?

    August 11th, 2008 @ 10:37 PM

  5. Brad Sumersford
    Brad Sumersford

    Ryan, the problem with the patch you submitted, it will not trap any of the find_by_* varieties. Leads back to your point about the regular expression being ugly and the side effects that come with it.

    August 13th, 2008 @ 04:52 PM

  6. Ryan Bates
    Ryan Bates

    Brad, are you certain? For one thing find_by uses method missing, so they aren't really methods. Another thing is that we're looping through the methods on Array here, not methods on models. There are only two methods with "find" in them in Array:

    
    [].methods.grep(/find/) # => ["find", "find_all"]
    

    August 13th, 2008 @ 05:55 PM

  7. Brad Sumersford
    Brad Sumersford

    Ryan, you are correct, I was just seeing any potential method (from monkey patching array before named_scope is evaluated) and not just array methods when I thought of that. But that's really a fringe case and not applicable.

    Thanks.

    August 14th, 2008 @ 06:24 PM

  8. Repository
    Repository
    • State changed from new to resolved

    (from [2415652660242d6b0da97119c562ecff82928575]) Support find_all on named scopes. [#730 Named Scope does not respond to array method find_all state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    August 21st, 2008 @ 12:38 PM

  9. Repository
    Repository

    (from [38a0d5c0c7a37e22f9376c7cc363f4cf4f19a3d4]) Support find_all on named scopes. [#730 Named Scope does not respond to array method find_all state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    August 25th, 2008 @ 09:30 AM