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

Check whether the current attribute being write is aliased or not before writing

- If aliased, then use the aliased attribute name.
This commit is contained in:
Prathamesh Sonpatki 2016-10-05 22:04:33 +05:30
parent 9cbf54c81a
commit 44dc4f6645
No known key found for this signature in database
GPG key ID: 8B90F6B89E2BCB71
3 changed files with 20 additions and 2 deletions

View file

@ -1,3 +1,8 @@
* Fix `write_attribute` method to check whether an attribute is aliased or not, and
use the aliased attribute name if needed.
*Prathamesh Sonpatki*
* Fix `read_attribute` method to check whether an attribute is aliased or not, and
use the aliased attribute name if needed.
@ -65,7 +70,7 @@
*Jon Moss*
* Add `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.
* Added `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.
Example:

View file

@ -29,7 +29,13 @@ module ActiveRecord
# specified +value+. Empty strings for Integer and Float columns are
# turned into +nil+.
def write_attribute(attr_name, value)
write_attribute_with_type_cast(attr_name, value, true)
name = if self.class.attribute_alias?(attr_name)
self.class.attribute_alias(attr_name).to_s
else
attr_name.to_s
end
write_attribute_with_type_cast(name, value, true)
end
def raw_write_attribute(attr_name, value) # :nodoc:

View file

@ -319,6 +319,13 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert_equal "Still another topic: part 4", topic.title
end
test "write_attribute can write aliased attributes as well" do
topic = Topic.new(title: "Don't change the topic")
topic.write_attribute :heading, "New topic"
assert_equal "New topic", topic.title
end
test "read_attribute" do
topic = Topic.new
topic.title = "Don't change the topic"