aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb12
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb5
-rwxr-xr-xactiverecord/test/associations_test.rb45
4 files changed, 59 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 0301572b23..6e69f3cd6a 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 23ccb34cd2..b2a2ed6e11 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -449,11 +449,13 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index 60c31ce486..fd36c2d8ae 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -32,12 +32,17 @@ module ActiveRecord
@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
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 35e14a5e19..763b66da9b 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -794,6 +794,7 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
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 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
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)