Skip to content

Commit

Permalink
fix the dirty tracking code's save hook overwriting missing attribute…
Browse files Browse the repository at this point in the history
…s with initialized-to-nil attributes. fixes #29017.
  • Loading branch information
willbryant committed May 9, 2017
1 parent 943efa3 commit ba2190b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def initialized?
false
end

def forgetting_assignment
dup
end

def with_type(type)
self.class.new(name, type)
end
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,13 @@ def catchphrase
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")

Expand Down

0 comments on commit ba2190b

Please sign in to comment.