123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Key::Composite

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Key
Instance Chain:
self, Key
Inherits: Key
  • ::Object
Defined in: activerecord/lib/active_record/key.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(columns) ⇒ Composite

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 123

def initialize(columns)
  @columns = columns.map { |column| -column.to_s }.freeze
  @name = @columns
end

Instance Attribute Details

#composite?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 128

def composite?
  true
end

Instance Method Details

#arel_columns(table)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 136

def arel_columns(table)
  @columns.map { |column| table[column] }
end

#cast(values, model)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 140

def cast(values, model)
  @columns.zip(values).map! { |column, value| model.type_for_attribute(column).cast(value) }
end

#expects_multiple_ids?(value) ⇒ Boolean

A single composite id is itself an ::Array, so several ids are an ::Array of Arrays.

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 150

def expects_multiple_ids?(value)
  value.first.is_a?(Array)
end

#inferred_id

When a composite key has the conventional [tenant_key, "id"] shape, associations join on "id" alone; otherwise the whole key is used.

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 156

def inferred_id
  @columns.include?("id") ? "id" : @name
end

#value_of(record)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 144

def value_of(record)
  @columns.map { |column| record._read_attribute(column) }
end

#where_clauses(values)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 160

def where_clauses(values)
  values.map { |set| where_hash(set) }
end

#where_hash(values)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 132

def where_hash(values)
  @columns.zip(values).to_h
end