提交 7d65f7cd 编写于 作者: J Jamis Buck

Revert [2040], caused assignment of belongs_to associations to fail in some cases


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2048 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 943fde0a
*SVN*
* Saving a record with two unsaved belongs_to associations pointing to the same object fails #2023 [Tobias Luetke]
* Make destroy return self #1913 [sebastian.kanthak@muehlheim.de]
* Fix typo in validations documentation #1938 [court3nay]
......
......@@ -445,12 +445,10 @@ def belongs_to(association_id, options = {})
module_eval do
before_save <<-EOF
association = instance_variable_get("@#{association_name}")
if not association.nil?
if association.new_record?
association.save(true)
association.send(:construct_sql)
end
if not association.nil? and association.new_record?
association.save(true)
self["#{association_class_primary_key_name}"] = association.id
association.send(:construct_sql)
end
EOF
end
......
......@@ -7,6 +7,8 @@
require 'fixtures/computer'
require 'fixtures/customer'
require 'fixtures/order'
require 'fixtures/post'
require 'fixtures/author'
# Can't declare new classes in test case methods, so tests before that
bad_collection_keys = false
......@@ -813,12 +815,21 @@ def test_store_association_in_two_relations_with_one_save
end
def test_association_assignment_sticks
client = Client.find(:first)
apple = Firm.create("name" => "Apple")
client.firm = apple
client.save!
client.reload
assert_equal apple.id, client.firm_id
post = Post.find(:first)
author1, author2 = Author.find(:all, :limit => 2)
# make sure the association is loaded
post.author
# set the association by id, directly
post.author_id = author2.id
# save and reload
post.save!
post.reload
# the author id of the post should be the id we set
assert_equal post.author_id, author2.id
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册