Module: ActiveRecord::AttributeMethods::Read

Defined in:
lib/composite_primary_keys/read.rb

Instance Method Summary collapse

Instance Method Details

#read_attribute(attr_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/composite_primary_keys/read.rb', line 4

def read_attribute(attr_name)
  attr_name = attr_name.to_s
  # CPK
  # attr_name = self.class.primary_key if attr_name == 'id'
  attr_name = self.class.primary_key if (attr_name == 'id' and !self.composite?)
  if !(value = @attributes[attr_name]).nil?
    if column = column_for_attribute(attr_name)
      if unserializable_attribute?(attr_name, column)
        unserialize_attribute(attr_name)
      else
        column.type_cast(value)
      end
    else
      value
    end
  else
    nil
  end
end