Permalink
Browse files

Allow polymorphic has_one to work when the association is set before …

…the owner has been saved. Fixes #1524.
  • Loading branch information...
1 parent 4f5f6a3 commit 7d1782a2c15094224986e60eb15e864f8ea18e37 @jonleighton jonleighton committed Jun 7, 2011
@@ -177,9 +177,7 @@ def creation_attributes
# 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
@@ -145,6 +145,17 @@ def test_set_polymorphic_has_one
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))

0 comments on commit 7d1782a

Please sign in to comment.