diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-06-08 00:32:40 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-06-08 00:32:49 +0100 |
commit | 7d1782a2c15094224986e60eb15e864f8ea18e37 (patch) | |
tree | 0facde04f119c121ca959e2d14e75e18ddf04226 /activerecord | |
parent | 4f5f6a3257862c0893c89a69321025e5873585dc (diff) | |
download | rails-7d1782a2c15094224986e60eb15e864f8ea18e37.tar.gz rails-7d1782a2c15094224986e60eb15e864f8ea18e37.tar.bz2 rails-7d1782a2c15094224986e60eb15e864f8ea18e37.zip |
Allow polymorphic has_one to work when the association is set before the owner has been saved. Fixes #1524.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/association.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/associations/join_model_test.rb | 11 |
2 files changed, 12 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 0ccf74c7a4..c3aee90124 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -177,9 +177,7 @@ module ActiveRecord # Sets the owner attributes on the given record def set_owner_attributes(record) - if owner.persisted? - creation_attributes.each { |key, value| record[key] = value } - end + creation_attributes.each { |key, value| record[key] = value } end # Should be true if there is a foreign key present on the owner which diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 785ea079ed..f22a91be2d 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -145,6 +145,17 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_equal posts(:thinking), tagging.taggable end + def test_set_polymorphic_has_one_on_new_record + tagging = tags(:misc).taggings.create + post = Post.new :title => "foo", :body => "bar" + post.tagging = tagging + post.save! + + assert_equal "Post", tagging.taggable_type + assert_equal post.id, tagging.taggable_id + assert_equal post, tagging.taggable + end + def test_create_polymorphic_has_many_with_scope old_count = posts(:welcome).taggings.count tagging = posts(:welcome).taggings.create(:tag => tags(:misc)) |