aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-02-08 14:59:48 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-02-08 14:59:48 -0800
commit69e84e10ec5b92de932ad0dc3497362f45a27edc (patch)
tree44af26e55b2759d300f7f59cecb3ed385db9d787 /activerecord/lib
parent3f97a97fc0512967ec61017cd727d1b8f3b63738 (diff)
downloadrails-69e84e10ec5b92de932ad0dc3497362f45a27edc.tar.gz
rails-69e84e10ec5b92de932ad0dc3497362f45a27edc.tar.bz2
rails-69e84e10ec5b92de932ad0dc3497362f45a27edc.zip
return early if the cast attribute has been cached
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb27
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