Module: ActiveModel::AttributeAssignment

Defined in:
lib/composite_primary_keys/active_model/attribute_assignment.rb

Instance Method Summary collapse

Instance Method Details

#_assign_attribute(k, v) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/composite_primary_keys/active_model/attribute_assignment.rb', line 3

def _assign_attribute(k, v)
  # CPK. This is super ugly, but if a table has a composite key where one of the fields is named :id we need
  # to handle it as a single value. Otherwise, we would call the id=(value) method which is expecting
  # and array of values.
  if k == 'id' && !v.kind_of?(Array) && self.kind_of?(ActiveRecord::Base) && self.composite? && !self.column_for_attribute(k).null
    self._write_attribute(k, v)
  else
    setter = :"#{k}="
    if respond_to?(setter)
      public_send(setter, v)
    else
      raise UnknownAttributeError.new(self, k)
    end
  end
end