提交 0e3a54a3 编写于 作者: R Rick Olson

Don't unnecessarily load has_many associations in after_update callbacks. ...

Don't unnecessarily load has_many associations in after_update callbacks.  Closes #6822 [stopdropandrew, canadaduane]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8504 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 e8730713
*SVN*
* Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 [stopdropandrew, canadaduane]
* Eager belongs_to :include infers the foreign key from the association name rather than the class name. #10517 [Jonathan Viney]
* SQLite: fix rename_ and remove_column for columns with unique indexes. #10576 [Brandon Keepers]
......
......@@ -1079,8 +1079,10 @@ def add_multiple_associated_save_callbacks(association_name)
if association.respond_to?(:loaded?)
if new_record?
association
else
elsif association.loaded?
association.select { |record| record.new_record? }
else
association.target.select { |record| record.new_record? }
end.each do |record|
errors.add "#{association_name}" unless record.valid?
end
......@@ -1097,6 +1099,8 @@ def add_multiple_associated_save_callbacks(association_name)
association
elsif association.respond_to?(:loaded?) && association.loaded?
association.select { |record| record.new_record? }
elsif association.respond_to?(:loaded?) && !association.loaded?
association.target.select { |record| record.new_record? }
else
[]
end
......
......@@ -72,23 +72,23 @@ def test_storing_in_pstore
end
end
end
class AssociationProxyTest < Test::Unit::TestCase
fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects
def test_proxy_accessors
welcome = posts(:welcome)
assert_equal welcome, welcome.author.proxy_owner
assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection
welcome.author.class # force load target
assert_equal welcome.author, welcome.author.proxy_target
david = authors(:david)
assert_equal david, david.posts.proxy_owner
assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
david.posts.first # force load target
assert_equal david.posts, david.posts.proxy_target
assert_equal david, david.posts_with_extension.testing_proxy_owner
assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
david.posts_with_extension.first # force load target
......@@ -98,10 +98,28 @@ def test_proxy_accessors
def test_push_does_not_load_target
david = authors(:david)
david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!"))
assert !david.posts.loaded?
assert david.posts.include?(post)
end
def test_push_has_many_through_does_not_load_target
david = authors(:david)
david.categories << categories(:technology)
assert !david.categories.loaded?
assert david.categories.include?(categories(:technology))
end
def test_push_followed_by_save_does_not_load_target
david = authors(:david)
david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!"))
assert !david.posts.loaded?
david.save
assert !david.posts.loaded?
assert david.posts.include?(post)
end
def test_push_does_not_lose_additions_to_new_record
josh = Author.new(:name => "Josh")
......@@ -772,7 +790,13 @@ def test_build_many
assert companies(:first_firm).save
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
def test_build_followed_by_save_does_not_load_target
new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
assert companies(:first_firm).save
assert !companies(:first_firm).clients_of_firm.loaded?
end
def test_build_without_loading_association
first_topic = topics(:first)
Reply.column_names
......@@ -825,6 +849,12 @@ def test_create_many
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
def test_create_followed_by_save_does_not_load_target
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
assert companies(:first_firm).save
assert !companies(:first_firm).clients_of_firm.loaded?
end
def test_find_or_initialize
the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
assert_equal companies(:first_firm).id, the_client.firm_id
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册