1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #34569 from gmcgibbon/allow_attribute_aliases_in_update

Allow aliased attributes in update
This commit is contained in:
Rafael França 2018-11-29 15:31:24 -05:00 committed by GitHub
commit 5b75408e32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View file

@ -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.

View file

@ -158,9 +158,13 @@ module ActiveRecord
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

View file

@ -323,6 +323,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase
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"