Skip to content

Commit

Permalink
Merge pull request #34569 from gmcgibbon/allow_attribute_aliases_in_u…
Browse files Browse the repository at this point in the history
…pdate

Allow aliased attributes in update
  • Loading branch information
rafaelfranca committed Nov 29, 2018
2 parents c8e4d5a + 72e63c7 commit 5b75408
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Allow aliased attributes to be used in `#update_columns` and `#update`.

*Gannon McGibbon*

* Allow spaces in postgres table names.

Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
Expand Down
10 changes: 7 additions & 3 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -158,9 +158,13 @@ def attributes_in_database
end

private
def write_attribute_without_type_cast(attr_name, _)
result = super
clear_attribute_change(attr_name)
def write_attribute_without_type_cast(attr_name, value)
name = attr_name.to_s
if self.class.attribute_alias?(name)
name = self.class.attribute_alias(name)
end
result = super(name, value)
clear_attribute_change(name)
result
end

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -323,6 +323,12 @@ def setup
assert_raises(ActiveModel::UnknownAttributeError) { topic.update(no_column_exists: "Hello!") }
end

test "write_attribute allows writing to aliased attributes" do
topic = Topic.first
assert_nothing_raised { topic.update_columns(heading: "Hello!") }
assert_nothing_raised { topic.update(heading: "Hello!") }
end

test "read_attribute" do
topic = Topic.new
topic.title = "Don't change the topic"
Expand Down

0 comments on commit 5b75408

Please sign in to comment.