aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-11-11 02:26:23 -0800
committerYves Senn <yves.senn@gmail.com>2013-11-11 02:26:23 -0800
commitf5802708db9480624b611975ef190dcc1b9443c5 (patch)
tree1417700fccb3e871c68d1cf5224e56965b2092c9 /activerecord/test
parent9222928dfb8aee5da2266b36ab7cfff8789f7022 (diff)
parent3ed5642e69b9a132c78bc0b1e54cbdc3753d8a94 (diff)
downloadrails-f5802708db9480624b611975ef190dcc1b9443c5.tar.gz
rails-f5802708db9480624b611975ef190dcc1b9443c5.tar.bz2
rails-f5802708db9480624b611975ef190dcc1b9443c5.zip
Merge pull request #12834 from dredozubov/has_one_association_replacement
Fixes problem with replacing has_one association record with itself
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb18
1 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index cdd386187b..a7a8e0c5c6 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -509,16 +509,30 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_no_queries { Firm.new.account = account }
end
- def test_has_one_assignment_triggers_save_on_change
+ def test_has_one_assignment_dont_triggers_save_on_change_of_same_object
pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
ship = pirate.build_ship(name: 'old name')
ship.save!
ship.name = 'new name'
assert ship.changed?
+ assert_queries(1) do
+ # One query for updating name, not triggering query for updating pirate_id
+ pirate.ship = ship
+ end
+
+ assert_equal 'new name', pirate.ship.reload.name
+ end
+
+ def test_has_one_assignment_triggers_save_on_change_on_replacing_object
+ pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
+ ship = pirate.build_ship(name: 'old name')
+ ship.save!
+
+ new_ship = Ship.create(name: 'new name')
assert_queries(2) do
# One query for updating name and second query for updating pirate_id
- pirate.ship = ship
+ pirate.ship = new_ship
end
assert_equal 'new name', pirate.ship.reload.name