From ba2190b3b6e506d7e5cede710594b22f9d937f39 Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Tue, 9 May 2017 22:04:22 +1200 Subject: [PATCH] fix the dirty tracking code's save hook overwriting missing attributes with initialized-to-nil attributes. fixes #29017. --- activerecord/lib/active_record/attribute.rb | 4 ++++ activerecord/test/cases/dirty_test.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 38281158d8..5b4e967e94 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -231,6 +231,10 @@ module ActiveRecord false end + def forgetting_assignment + dup + end + def with_type(type) self.class.new(name, type) end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 721861975a..1fc78f8d32 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -775,6 +775,13 @@ class DirtyTest < ActiveRecord::TestCase assert person.changed? end + test "attributes not selected are still missing after save" do + person = Person.select(:id).first + assert_raises(ActiveModel::MissingAttributeError) { person.first_name } + assert person.save # calls forget_attribute_assignments + assert_raises(ActiveModel::MissingAttributeError) { person.first_name } + end + test "saved_change_to_attribute? returns whether a change occurred in the last save" do person = Person.create!(first_name: "Sean")