mirror of
https://github.com/github/rails.git
synced 2026-01-29 16:28:09 -05:00
Don't save has_one associations unnecessarily. Closes #5735.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4690 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Don't save has_one associations unnecessarily. #5735 [Jonathan Viney]
|
||||
|
||||
* Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. [Rick Olson]
|
||||
|
||||
* Formally deprecate the deprecated finders. [Koz]
|
||||
|
||||
@@ -584,7 +584,7 @@ module ActiveRecord
|
||||
module_eval do
|
||||
after_save <<-EOF
|
||||
association = instance_variable_get("@#{reflection.name}")
|
||||
unless association.nil?
|
||||
if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
|
||||
association["#{reflection.primary_key_name}"] = id
|
||||
association.save(true)
|
||||
end
|
||||
|
||||
@@ -291,6 +291,23 @@ class HasOneAssociationsTest < Test::Unit::TestCase
|
||||
assert_equal a, firm.account
|
||||
assert_equal a, firm.account(true)
|
||||
end
|
||||
|
||||
def test_not_resaved_when_unchanged
|
||||
firm = Firm.find(:first, :include => :account)
|
||||
assert_queries(1) { firm.save! }
|
||||
|
||||
firm = Firm.find(:first)
|
||||
firm.account = Account.find(:first)
|
||||
assert_queries(1) { firm.save! }
|
||||
|
||||
firm = Firm.find(:first).clone
|
||||
firm.account = Account.find(:first)
|
||||
assert_queries(2) { firm.save! }
|
||||
|
||||
firm = Firm.find(:first).clone
|
||||
firm.account = Account.find(:first).clone
|
||||
assert_queries(2) { firm.save! }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user