未验证 提交 a91f2ed7 编写于 作者: R Rafael França 提交者: Rafael Mendonça França

Merge pull request #34569 from gmcgibbon/allow_attribute_aliases_in_update

Allow aliased attributes in update
上级 8bdf2173
* 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.
......
......@@ -120,9 +120,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
......
......@@ -329,6 +329,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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册