From 69e84e10ec5b92de932ad0dc3497362f45a27edc Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Feb 2012 14:59:48 -0800 Subject: return early if the cast attribute has been cached --- .../lib/active_record/attribute_methods/read.rb | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 54a5258e4c..eff69cf174 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -119,19 +119,24 @@ module ActiveRecord # Returns the value of the attribute identified by attr_name after it has been typecast (for example, # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). def read_attribute(attr_name) - column = @columns_hash.fetch(attr_name) { - return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache) - } + # If it's cached, just return it + @attributes_cache.fetch(attr_name) { - value = @attributes.fetch(attr_name) { - return block_given? ? yield(attr_name) : nil - } + column = @columns_hash.fetch(attr_name) { + return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache) + } - if self.class.cache_attribute?(attr_name) - @attributes_cache[attr_name] ||= column.type_cast(value) - else - column.type_cast value - end + value = @attributes.fetch(attr_name) { + return block_given? ? yield(attr_name) : nil + } + + if self.class.cache_attribute?(attr_name) + @attributes_cache[attr_name] ||= column.type_cast(value) + else + column.type_cast value + end + + } end private -- cgit v1.2.3