Module: ActiveModelPersistence::PrimaryKey::ClassMethods

Defined in:
lib/active_model_persistence/primary_key.rb

Overview

When this module is included in another class, ActiveSupport::Concern will make these class methods on that class.

Instance Method Summary collapse

Instance Method Details

#primary_keySymbol

Identifies the attribute that the `primary_key` accessor maps to

The primary key is 'id' by default.

Examples:

class Employee
  include ActiveModelPersistence::PrimaryKey
  attribute :username, :string
  self.primary_key = :username
end
Employee.primary_key #=> :username

Returns:

  • (Symbol)

    the attribute that the `primary_key` accessor is an alias for



59
60
61
# File 'lib/active_model_persistence/primary_key.rb', line 59

def primary_key
  @primary_key ||= 'id'
end

#primary_key=(attribute) ⇒ void

This method returns an undefined value.

Sets the attribute to use for the primary key

Examples:

class Employee
  include ActiveModelPersistence::PrimaryKey
  attribute :username, :string
  primary_key = :username
end
e = Employee.new(username: 'couballj')
e.primary_key #=> 'couballj'

Parameters:

  • attribute (Symbol)

    the attribute to use for the primary key



78
79
80
# File 'lib/active_model_persistence/primary_key.rb', line 78

def primary_key=(attribute)
  @primary_key = attribute.to_s
end