aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPrathamesh Sonpatki <csonpatki@gmail.com>2016-10-05 22:04:33 +0530
committerPrathamesh Sonpatki <csonpatki@gmail.com>2016-12-09 00:00:50 +0530
commit44dc4f6645e12ddd5cf927bca0675a5b44d55cbd (patch)
treeed8d0a3bcb5292d13186e16fac1c5c7ab73a46d3 /activerecord
parent9cbf54c81a46cc070f3997956c12915f39dbb46b (diff)
downloadrails-44dc4f6645e12ddd5cf927bca0675a5b44d55cbd.tar.gz
rails-44dc4f6645e12ddd5cf927bca0675a5b44d55cbd.tar.bz2
rails-44dc4f6645e12ddd5cf927bca0675a5b44d55cbd.zip
Check whether the current attribute being write is aliased or not before writing
- If aliased, then use the aliased attribute name.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb8
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb7
3 files changed, 20 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 8b9396936a..d7c2d80f33 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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:
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index f65c297e01..0022d526a4 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -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:
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index a89ecc32a6..4ac604a164 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -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"