diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-10-08 06:45:32 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-10-08 06:45:32 +0000 |
commit | 0c3c131f3b9a82d3e2f36b2930c3437c9f821b6d (patch) | |
tree | 4cd7f72d3f4f6b5197185b3efa987ecd0cd08097 /activerecord | |
parent | 6c0609fafb387f2f61ba9eb9754c31bdebeb46ec (diff) | |
download | rails-0c3c131f3b9a82d3e2f36b2930c3437c9f821b6d.tar.gz rails-0c3c131f3b9a82d3e2f36b2930c3437c9f821b6d.tar.bz2 rails-0c3c131f3b9a82d3e2f36b2930c3437c9f821b6d.zip |
The has_many create method works with polymorphic associations. Closes #6361.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 9 | ||||
-rw-r--r-- | activerecord/test/associations/join_model_test.rb | 7 |
3 files changed, 14 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 9524643113..f2b377a20b 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* The has_many create method works with polymorphic associations. #6361 [Dan Peterson] + * MySQL: introduce Mysql::Result#all_hashes to support further optimization. #5581 [Stefan Kaes] * save! shouldn't validate twice. #6324 [maiha, Bob Silva] diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index d0af68eea7..3cd6e5d716 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -91,20 +91,21 @@ module ActiveRecord @reflection.klass.find(*args) end end - + protected def method_missing(method, *args, &block) if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) super else + create_scoping = {} + set_belongs_to_association_for(create_scoping) + @reflection.klass.with_scope( + :create => create_scoping, :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false - }, - :create => { - @reflection.primary_key_name => @owner.id } ) do @reflection.klass.send(method, *args, &block) diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb index fb8df9c21c..c67956fba3 100644 --- a/activerecord/test/associations/join_model_test.rb +++ b/activerecord/test/associations/join_model_test.rb @@ -137,6 +137,13 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal "Post", tagging.taggable_type assert_equal old_count+1, posts(:welcome).taggings.count end + + def test_create_bang_polymorphic_with_has_many_scope + old_count = posts(:welcome).taggings.count + tagging = posts(:welcome).taggings.create!(:tag => tags(:misc)) + assert_equal "Post", tagging.taggable_type + assert_equal old_count+1, posts(:welcome).taggings.count + end def test_create_polymorphic_has_one_with_scope old_count = Tagging.count |