From 86ea94e4d0c228c79b3709f0c667ba90b02e41cd Mon Sep 17 00:00:00 2001 From: Nick Howard Date: Sun, 1 May 2011 16:22:46 -0600 Subject: Fix for lighthouse #6741 - adds tests for find_or_create_by and find_or_initialize_by on has_many associations - changes the behavior of ActiveRecord::Associations::CollectionProxy#method_missing to differ to ActiveRecord::FinderMethods#find_or_instantiator_by_attributes for arg processing and saving so find_or_create_by's api on associations will be consistent w/ the api for model classes. --- activerecord/lib/active_record/associations/collection_proxy.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 388173c1fb..adfc71d435 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -64,9 +64,12 @@ module ActiveRecord def method_missing(method, *args, &block) match = DynamicFinderMatch.match(method) - if match && match.creator? - attributes = match.attribute_names - return send(:"find_by_#{attributes.join('_and_')}", *args) || create(Hash[attributes.zip(args)]) + if match && match.instantiator? + record = send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |r| + @association.send :set_owner_attributes, r + @association.send :add_to_target, r + yield(r) if block_given? + end end if target.respond_to?(method) || (!@association.klass.respond_to?(method) && Class.respond_to?(method)) -- cgit v1.2.3