This project is archived and is in readonly mode.
Named Scope does not respond to array method find_all
Reported by Brad Sumersford | July 31st, 2008 @ 03:36 PM | in 2.x
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
Comments and changes to this ticket
-
Brad Sumersford July 30th, 2008 @ 11:15 PM
Not sure why it puts a semicolon after the & in a highlighted source code section,
-
Brad Sumersford August 11th, 2008 @ 07:58 PM
Just using Topic.base.select(&:approved) would be a better workaround, but I still like my find_all :)
-
Ryan Bates August 11th, 2008 @ 10:37 PM
- no changes were found...
-
Ryan Bates August 11th, 2008 @ 10:37 PM
- 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?
-
Brad Sumersford August 13th, 2008 @ 04:52 PM
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.
-
Ryan Bates August 13th, 2008 @ 05:55 PM
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"]
-
Brad Sumersford August 14th, 2008 @ 06:24 PM
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.
-
Repository August 21st, 2008 @ 12:38 PM
- State changed from new to resolved
(from [2415652660242d6b0da97119c562ecff82928575]) Support find_all on named scopes. [#730 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
-
Repository August 25th, 2008 @ 09:30 AM
(from [38a0d5c0c7a37e22f9376c7cc363f4cf4f19a3d4]) Support find_all on named scopes. [#730 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.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
Tags
Referenced by
- 730 Named Scope does not respond to array method find_all (from [2415652660242d6b0da97119c562ecff82928575]) Support...
- 730 Named Scope does not respond to array method find_all (from [38a0d5c0c7a37e22f9376c7cc363f4cf4f19a3d4]) Support...