Permalink
Browse files

Fixed saving a record with two unsaved belongs_to associations pointi…

…ng to the same object #2023 [Tobias Luetke]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2278 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4fa9a2b commit 5213a1f73332995ae98d2a163454dff2b288e650 @jamis jamis committed Sep 20, 2005
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed saving a record with two unsaved belongs_to associations pointing to the same object #2023 [Tobias Luetke]
+
* Improved migrations' behavior when the schema_info table is empty. [Nicholas Seckar]
* Fixed that Observers didn't observe sub-classes #627 [Florian Weber]
@@ -449,11 +449,13 @@ def belongs_to(association_id, options = {})
module_eval do
before_save <<-EOF
association = instance_variable_get("@#{association_name}")
- if not association.nil? and association.new_record?
- association.save(true)
- self["#{association_class_primary_key_name}"] = association.id
- association.send(:construct_sql)
- end
+ if not association.nil?
+ if association.new_record?
+ association.save(true)
+ association.send(:construct_sql)
+ end
+ self["#{association_class_primary_key_name}"] = association.id if association.updated?
+ end
EOF
end
@@ -32,12 +32,17 @@ def replace(obj, dont_save = false)
@target = obj
@owner[@association_class_primary_key_name] = obj.id unless obj.new_record?
+ @updated = true
end
@loaded = true
return (@target.nil? ? nil : self)
end
+ def updated?
+ @updated
+ end
+
protected
@@ -794,6 +794,7 @@ def test_store_two_association_with_one_save
assert_equal num_orders +1, Order.count
assert_equal num_customers +2, Customer.count
end
+
def test_store_association_in_two_relations_with_one_save
num_orders = Order.count
@@ -814,6 +815,50 @@ def test_store_association_in_two_relations_with_one_save
assert_equal num_customers +1, Customer.count
end
+ def test_store_association_in_two_relations_with_one_save_in_existing_object
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.create
+
+ customer = order.billing = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ order.reload
+
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +1, Customer.count
+ end
+
+ def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.create
+
+ customer = order.billing = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ order.reload
+
+ customer = order.billing = order.shipping = Customer.new
+
+ assert order.save
+ order.reload
+
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +2, Customer.count
+ end
+
+
def test_association_assignment_sticks
post = Post.find(:first)
author1, author2 = Author.find(:all, :limit => 2)

0 comments on commit 5213a1f

Please sign in to comment.