[Please respect MFT to rails-core; I think most of the discussion of this,
if any, will be more topical there]

On Fri, Feb 10, 2006 at 01:33:32PM +1100, Matthew Palmer wrote:
> class Collection << ActiveRecord::Base
>   def findpkg
>     self.repositories.each do |r|
>       p = r.packages.find(:all, :conditions => "name like '%pkg%'")
>     end
>   end
> end

The bug is that HasManyAssociation#find modifies it's arguments, so the
above (slightly fictionalised) variant is misleading; the real code is:

def findpkg(*args)
  self.repositories.each do |r|
    p = r.packages.find(*args)
    # Do things with p
  end
end

Adding "puts args.inspect" before and after the call to find() shows that
args is in fact being modified, by the fact that the options object is the
same as args[1] on the entry to HasManyAssociation#find.  This simple patch
makes the whole problem go away:

------8<------
--- has_many_association.rb     2006-02-10 16:37:32.000000000 +1100
+++ has_many_association.rb.orig        2006-02-10 16:37:21.000000000 +1100
@@ -51,7 +51,7 @@
       end
 
       def find(*args)
-        options = Base.send(:extract_options_from_args!, args).dup
+        options = Base.send(:extract_options_from_args!, args)
 
         # If using a custom finder_sql, scan the entire collection.
         if @options[:finder_sql]
------>8------

I'm planning on reporting this in Trac, but I'm curious about people's
opinions -- is it reasonable to assume that arguments which appear, on the
surface, to be read-only may be modified by called methods?

- Matt
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to