Module: ActiveRecord::AttributeMethods::Read::ClassMethods

Defined in:
lib/active_record/attribute_methods/read.rb

Instance Method Summary collapse

Instance Method Details

#cache_attribute?(attr_name) ⇒ Boolean

Returns true if the provided attribute is being cached.

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_record/attribute_methods/read.rb', line 34

def cache_attribute?(attr_name)
  cached_attributes.include?(attr_name)
end

#cache_attributes(*attribute_names) ⇒ Object

cache_attributes allows you to declare which converted attribute values should be cached. Usually caching only pays off for attributes with expensive conversion methods, like time related columns (e.g. created_at, updated_at).



22
23
24
# File 'lib/active_record/attribute_methods/read.rb', line 22

def cache_attributes(*attribute_names)
  attribute_names.each {|attr| cached_attributes << attr.to_s}
end

#cached_attributesObject

Returns the attributes which are cached. By default time related columns with datatype :datetime, :timestamp, :time, :date are cached.



28
29
30
31
# File 'lib/active_record/attribute_methods/read.rb', line 28

def cached_attributes
  @cached_attributes ||=
    columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map{|col| col.name}.to_set
end