aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPrathamesh Sonpatki <csonpatki@gmail.com>2016-09-16 19:21:56 +0530
committerPrathamesh Sonpatki <csonpatki@gmail.com>2016-12-08 23:59:10 +0530
commit9cbf54c81a46cc070f3997956c12915f39dbb46b (patch)
tree37f46f943cfa46866cd654cc7d60f043f08d0e96
parent17b09f4fca976063187be6494e64430850c37632 (diff)
downloadrails-9cbf54c81a46cc070f3997956c12915f39dbb46b.tar.gz
rails-9cbf54c81a46cc070f3997956c12915f39dbb46b.tar.bz2
rails-9cbf54c81a46cc070f3997956c12915f39dbb46b.zip
Check whether the current attribute being read is aliased or not before reading
- If aliased, then use the aliased attribute name. - Fixes #26417.
-rw-r--r--activerecord/CHANGELOG.md9
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb7
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb10
3 files changed, 24 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 2182ee78bb..8b9396936a 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Fix `read_attribute` method to check whether an attribute is aliased or not, and
+ use the aliased attribute name if needed.
+
+ Fixes #26417.
+
+ *Prathamesh Sonpatki*
+
* PostgreSQL & MySQL: Use big integer as primary key type for new tables
*Jon McCartie*, *Pavel Pravosud*
@@ -58,7 +65,7 @@
*Jon Moss*
-* Added `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`
+* Add `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.
Example:
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 30f7750884..5448ebc165 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -48,7 +48,12 @@ module ActiveRecord
# it has been typecast (for example, "2004-12-12" in a date column is cast
# to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name, &block)
- name = attr_name.to_s
+ name = if self.class.attribute_alias?(attr_name)
+ self.class.attribute_alias(attr_name).to_s
+ else
+ attr_name.to_s
+ end
+
name = self.class.primary_key if name == "id".freeze
_read_attribute(name, &block)
end
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index ba6877a6a6..a89ecc32a6 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -329,6 +329,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert_equal "Don't change the topic", topic[:title]
end
+ test "read_attribute can read aliased attributes as well" do
+ topic = Topic.new(title: "Don't change the topic")
+
+ assert_equal "Don't change the topic", topic.read_attribute("heading")
+ assert_equal "Don't change the topic", topic["heading"]
+
+ assert_equal "Don't change the topic", topic.read_attribute(:heading)
+ assert_equal "Don't change the topic", topic[:heading]
+ end
+
test "read_attribute raises ActiveModel::MissingAttributeError when the attribute does not exist" do
computer = Computer.select("id").first
assert_raises(ActiveModel::MissingAttributeError) { computer[:developer] }