diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-08 14:59:48 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-08 14:59:48 -0800 |
commit | 69e84e10ec5b92de932ad0dc3497362f45a27edc (patch) | |
tree | 44af26e55b2759d300f7f59cecb3ed385db9d787 /activerecord/lib/active_record/attribute_methods | |
parent | 3f97a97fc0512967ec61017cd727d1b8f3b63738 (diff) | |
download | rails-69e84e10ec5b92de932ad0dc3497362f45a27edc.tar.gz rails-69e84e10ec5b92de932ad0dc3497362f45a27edc.tar.bz2 rails-69e84e10ec5b92de932ad0dc3497362f45a27edc.zip |
return early if the cast attribute has been cached
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/read.rb | 27 |
1 files changed, 16 insertions, 11 deletions
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 <tt>attr_name</tt> 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 |