Fixed saving a record with two unsaved belongs_to associations pointing to the same object #2023 [Tobias Luetke]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2278 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
4fa9a2bfe2
commit
5213a1f733
|
@ -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 @@ 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue