From 9f48e75ad713a657b1e37cf22fa361ff98a4580a Mon Sep 17 00:00:00 2001 From: Tu Hoang Date: Thu, 30 Oct 2014 22:25:01 +0700 Subject: =?UTF-8?q?Add=20specs=20for=20adding-to/clear=20has=5Fmany=20coll?= =?UTF-8?q?ections=E2=80=99s=20behavior=20on=20`updated=5Fat`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are behaviors mentioned in #17161 that: 1. are not documented properly, and 2. don't have specs This commit addresses the spec absence. For has_many collections, 1. addition (<<) should update the associated object's updated_at (if any) 2. .clear, depending on options[:dependent], calls delete_all, destroy_all, or nullifies the associated object(s)' foreign key. --- .../associations/has_many_associations_test.rb | 25 ++++++++++++++++++++++ activerecord/test/schema/schema.rb | 1 + 2 files changed, 26 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index e34b993029..1699f5f95d 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -28,6 +28,7 @@ require 'models/college' require 'models/student' require 'models/pirate' require 'models/ship' +require 'models/ship_part' require 'models/tyre' class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase @@ -83,6 +84,30 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal college.students, Student.where(active: true, college_id: college.id) end + def test_add_record_to_collection_should_change_its_updated_at + ship = Ship.create(name: 'dauntless') + part = ShipPart.create(name: 'cockpit') + updated_at = part.updated_at + + ship.parts << part + + assert_equal part.ship, ship + assert_not_equal part.updated_at, updated_at + end + + def test_clear_collection_should_not_change_updated_at + # GH#17161: .clear calls delete_all (and returns the association), + # which is intended to not touch associated objects's updated_at field + ship = Ship.create(name: 'dauntless') + part = ShipPart.create(name: 'cockpit', ship_id: ship.id) + + ship.parts.clear + part.reload + + assert_equal nil, part.ship + assert !part.updated_at_changed? + end + def test_create_from_association_should_respect_default_scope car = Car.create(:name => 'honda') assert_equal 'honda', car.name diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 10de3d34cb..5e0c0b889a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -650,6 +650,7 @@ ActiveRecord::Schema.define do create_table :ship_parts, force: true do |t| t.string :name t.integer :ship_id + t.datetime :updated_at end create_table :speedometers, force: true, id: false do |t| -- cgit v1.2.3