Skip to content

Commit

Permalink
Merge pull request #29018 from willbryant/missing_attributes_after_save
Browse files Browse the repository at this point in the history
fix the dirty tracking code's save hook overwriting missing attribute…
  • Loading branch information
kamipo committed Jan 2, 2018
2 parents 921b877 + ba2190b commit a954e1e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,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 @@ -771,6 +771,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 a954e1e

Please sign in to comment.