Changes don't seem to appear in the #changes list for a newly added attribute

I’m not seeing any changes in the #changes hash after changing a newly added attribute:

Test [1] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.changes
=> {}
Test [2] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.first_name
=> nil
Test [3] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.first_name = "whatever"
=> "whatever"
Test [4] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.changes
=> {}
Test [5] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.save
=> true
Test [6] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.first_name
=> "whatever"
Test [7] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.first_name = "changed again"
=> "changed again"
Test [8] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.changes
=> {}

Changes don’t appear when I change anything for this attribute.

But, for attributes that have already existed before adding the new attribute, it seems to work:

Test [9] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.email
=> "test_1@example.com"
Test [10] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.email = "foo@bar.com"
=> "foo@bar.com"
Test [11] pry(#<RSpec::ExampleGroups::User::FirstOrLastNameChanged>)> subject.changes
=> {"email"=>["test_1@example.com", "foo@bar.com"]}

Am I missing something?

Thanks.

Looks like I had attr_accessor :first_name defined on my User model, and it seems that was interfering with the change tracking for that attribute.

1 Like