mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #17297 from rebyn/fix/17161-remove-objs-from-has_many-updates-fields
Add specs for adding-to/clear has_many collections’s behavior on `updated_at`
This commit is contained in:
commit
5c5d9417ca
2 changed files with 26 additions and 0 deletions
|
@ -30,6 +30,7 @@ require 'models/college'
|
|||
require 'models/student'
|
||||
require 'models/pirate'
|
||||
require 'models/ship'
|
||||
require 'models/ship_part'
|
||||
require 'models/tyre'
|
||||
require 'models/subscriber'
|
||||
require 'models/subscription'
|
||||
|
@ -162,6 +163,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
|
||||
|
|
|
@ -680,6 +680,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|
|
||||
|
|
Loading…
Reference in a new issue