module ActiveRecord::Base::CompositeInstanceMethods

Public Instance Methods

can_change_primary_key_values?() click to toggle source
# File lib/composite_primary_keys/base.rb, line 126
def can_change_primary_key_values?
  false
end
id() click to toggle source

A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.

# File lib/composite_primary_keys/base.rb, line 86
def id
  attr_names = self.class.primary_keys
  ::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) })
end
Also aliased as: ids
id=(ids) click to toggle source

Sets the primary ID.

# File lib/composite_primary_keys/base.rb, line 117
def id=(ids)
  ids = CompositePrimaryKeys::CompositeKeys.parse(ids)
  unless ids.length == self.class.primary_keys.length
    raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids"
  end
  [self.class.primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)}
  id
end
id_before_type_cast() click to toggle source
# File lib/composite_primary_keys/base.rb, line 110
def id_before_type_cast
  self.class.primary_keys.map do |key|
    self.read_attribute_before_type_cast(key)
  end
end
ids()
Alias for: id
ids_hash() click to toggle source
# File lib/composite_primary_keys/base.rb, line 103
def ids_hash
  self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)|
    hash[key] = value
    hash
  end
end
read_attribute_for_serialization(attribute) click to toggle source

This is overridden purely for json serialization support. If the model is composite and one of the keys is id, then we don’t want to call the id method, instead we want to get the id attribute value

# File lib/composite_primary_keys/base.rb, line 95
def read_attribute_for_serialization(attribute)
  if self.composite? && attribute == 'id'
    read_attribute(attribute)
  else
    send(attribute)
  end
end
to_key() click to toggle source

Returns this record’s primary keys values in an Array if any value is available

# File lib/composite_primary_keys/base.rb, line 132
def to_key
  ids.to_a if !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids
end