Module: ActiveModelPersistence::PrimaryKeyIndex::ClassMethods

Defined in:
lib/active_model_persistence/primary_key_index.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Create the primary key index



49
50
51
# File 'lib/active_model_persistence/primary_key_index.rb', line 49

def self.extended(base)
  base.index('primary_key', key_value_source: :primary_key, unique: true)
end

Instance Method Details

#find(primary_key_value) ⇒ Object?

Finds an object in the :primary_key index whose primary matches the given value

Examples:

class Employee
  include ActiveModelPersistence::PrimaryKeyIndex
  attribute :id, :integer
end
e1 = Employee.new(id: 1)
e1.update_indexes
Employee.find(1) #=> e1
Employee.find(2) #=> nil

Parameters:

  • primary_key_value (Object)

    The primary key value to find in the :primary_key index

Returns:

  • (Object, nil)

    The object in the :primary_key index whose primary matches the given value



39
40
41
# File 'lib/active_model_persistence/primary_key_index.rb', line 39

def find(primary_key_value)
  find_by_primary_key(primary_key_value).first
end