instance method define_read_method

Ruby on Rails 2.2.3

Since v2.2.3 Last seen in v2.3.18 Private

Available in: v2.2.3 v2.3.18

Signature

define_read_method(symbol, attr_name, column)

Define an attribute reader method. Cope with nil column.

Parameters

symbol req
attr_name req
column req
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 146
        def define_read_method(symbol, attr_name, column)
          cast_code = column.type_cast_code('v') if column
          access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"

          unless attr_name.to_s == self.primary_key.to_s
            access_code = access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
          end
          
          if cache_attribute?(attr_name)
            access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
          end
          evaluate_attribute_method attr_name, "def #{symbol}; #{access_code}; end"
        end

Defined in activerecord/lib/active_record/attribute_methods.rb line 146 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::AttributeMethods::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close