From 3f563f169612ec340816f0a549fe9db7ff95c238 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 29 Jun 2010 15:42:06 -0700 Subject: AssociationCollection#create_by_*, find_or_create_by_* work properly now. [#1108 state:resolved] Signed-off-by: Jeremy Kemper --- .../lib/active_record/associations/association_collection.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 186b531ffb..ddf4ce4058 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -409,6 +409,17 @@ module ActiveRecord end def method_missing(method, *args) + case method.to_s + when 'find_or_create' + return find(:first, :conditions => args.first) || create(args.first) + when /^find_or_create_by_(.*)$/ + rest = $1 + return send("find_by_#{rest}", *args) || + method_missing("create_by_#{rest}", *args) + when /^create_by_(.*)$/ + return create Hash[$1.split('_and_').zip(args)] + end + if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) if block_given? super { |*block_args| yield(*block_args) } -- cgit v1.2.3