Module: ActiveRecord::AttributeMethods::PrimaryKey

Extended by:
ActiveSupport::Concern
Defined in:
activerecord/lib/active_record/attribute_methods/primary_key.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included, prepend_features, prepended

Instance Method Details

#idObject

Returns the primary key column’s value.



18
19
20
21
22
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 18

def id
  return _read_attribute(@primary_key) unless @primary_key.is_a?(Array)

  @primary_key.map { |pk| _read_attribute(pk) }
end

#id=(value) ⇒ Object

Sets the primary key column’s value.



31
32
33
34
35
36
37
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 31

def id=(value)
  if self.class.composite_primary_key?
    @primary_key.zip(value) { |attr, value| _write_attribute(attr, value) }
  else
    _write_attribute(@primary_key, value)
  end
end

#id?Boolean

Queries the primary key column’s value.

Returns:

  • (Boolean)


40
41
42
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 40

def id?
  query_attribute(@primary_key)
end

#id_before_type_castObject

Returns the primary key column’s value before type cast.



45
46
47
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 45

def id_before_type_cast
  attribute_before_type_cast(@primary_key)
end

#id_for_databaseObject

:nodoc:



59
60
61
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 59

def id_for_database # :nodoc:
  @attributes[@primary_key].value_for_database
end

#id_in_databaseObject

Returns the primary key column’s value from the database.



55
56
57
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 55

def id_in_database
  attribute_in_database(@primary_key)
end

#id_wasObject

Returns the primary key column’s previous value.



50
51
52
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 50

def id_was
  attribute_was(@primary_key)
end

#primary_key_values_present?Boolean

:nodoc:

Returns:

  • (Boolean)


24
25
26
27
28
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 24

def primary_key_values_present? # :nodoc:
  return id.all? if self.class.composite_primary_key?

  !!id
end

#to_keyObject

Returns this record’s primary key value wrapped in an array if one is available.



12
13
14
15
# File 'activerecord/lib/active_record/attribute_methods/primary_key.rb', line 12

def to_key
  key = id
  [key] if key
end