rails/activerecord/test/models/mouse.rb
Will Jessop 6ea80b6103
Don't validate non dirty association targets
Fixes #36581.

This fixes an issue where validations would return differently when a previously saved invalid association was loaded between calls:

    assert_equal true, squeak.valid?
    assert_equal true, squeak.mouse.present?
    assert_equal true, squeak.valid?

Here the second assert would return

    Expected: true
    Actual: false

Limiting validations to associations that would be normally saved (using autosave: true) due to changes means that loading invalid associated relations will not change the return value of the parent relations's `valid?` method.
2019-07-15 18:21:20 +01:00

7 lines
138 B
Ruby

# frozen_string_literal: true
class Mouse < ActiveRecord::Base
has_many :squeaks, autosave: true
validates :name, presence: true
end