diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-06-29 15:42:06 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-06-29 16:13:40 -0700 |
commit | 3f563f169612ec340816f0a549fe9db7ff95c238 (patch) | |
tree | b825e646170355d6e15c895b0c0abcb9cc61b023 /activerecord/lib | |
parent | d7c2e52c6c04dec4e4665c8f0f9988bfe913cb15 (diff) | |
download | rails-3f563f169612ec340816f0a549fe9db7ff95c238.tar.gz rails-3f563f169612ec340816f0a549fe9db7ff95c238.tar.bz2 rails-3f563f169612ec340816f0a549fe9db7ff95c238.zip |
AssociationCollection#create_by_*, find_or_create_by_* work properly now. [#1108 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 11 |
1 files changed, 11 insertions, 0 deletions
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) } |