This project is archived and is in readonly mode.
named_scope fails when accessed through a class method over an association
-
Daniel Guettler
Looks like the scoped_methods get popped before the query is actually executed. The call graph for l.items.find_v2 and l.items.published is actually quite different and I couldn't get my head around it to figure out how to fix this the right way. However applying this change to the named_scope.rb file fixes the issue and doesn't brake any existing tests in rails version 2.2.2. I don't consider this at proper patch just as a temp fix in case someone needs it.
--- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -114,6 +114,7 @@ module ActiveRecord [options[:extend]].flatten.each { |extension| extend extension } if options[:extend] extend Module.new(&block) if block_given? @proxy_scope, @proxy_options = proxy_scope, options.except(:extend) + @scoped_methods = proxy_scope.send(:scoped_methods).dup end def reload @@ -166,9 +167,11 @@ module ActiveRecord if scopes.include?(method) scopes[method].call(self, *args) else - with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {} do - method = :new if method == :build - proxy_scope.send(method, *args, &block) + with_scope(@scoped_methods.last || {}) do + with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {} do + method = :new if method == :build + proxy_scope.send(method, *args, &block) + end end end end -
Daniel Guettler
Ah actually this bug is already resolved in couple other tickets. E.g. #1960