diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-06 19:23:40 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-06 19:23:40 +0000 |
commit | 35670bbde59de9d0838027bd4fcd4cc04f928e3d (patch) | |
tree | 838b2e56d702cf6b1f4ad78b956ea414510d19db /activerecord | |
parent | a27ccf176564db7293fba115a7d05087611ae84b (diff) | |
download | rails-35670bbde59de9d0838027bd4fcd4cc04f928e3d.tar.gz rails-35670bbde59de9d0838027bd4fcd4cc04f928e3d.tar.bz2 rails-35670bbde59de9d0838027bd4fcd4cc04f928e3d.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 17 |
3 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 9b69dce47c..9e63b108b3 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 192c7b7a5f..009153d1d2 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -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 diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 747d04cf3f..eecb096521 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -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 |