Module: ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods

Defined in:
lib/active_record/attribute_methods/primary_key.rb

Instance Method Summary collapse

Instance Method Details

#get_primary_key(base_name) ⇒ Object

:nodoc:



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

def get_primary_key(base_name) #:nodoc:
  key = 'id'
  case primary_key_prefix_type
    when :table_name
      key = base_name.to_s.foreign_key(false)
    when :table_name_with_underscore
      key = base_name.to_s.foreign_key
  end
  key
end

#primary_keyObject

Defines the primary key field – can be overridden in subclasses. Overwriting will negate any effect of the primary_key_prefix_type setting, though.



15
16
17
# File 'lib/active_record/attribute_methods/primary_key.rb', line 15

def primary_key
  reset_primary_key
end

#quoted_primary_keyObject

Returns a quoted version of the primary key name, used to construct SQL statements.



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

def quoted_primary_key
  @quoted_primary_key ||= connection.quote_column_name(primary_key)
end

#reset_primary_keyObject

:nodoc:



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

def reset_primary_key #:nodoc:
  key = get_primary_key(base_class.name)
  set_primary_key(key)
  key
end

#set_primary_key(value = nil, &block) ⇒ Object Also known as: primary_key=

Sets the name of the primary key column to use to the given value, or (if the value is nil or false) to the value returned by the given block.

class Project < ActiveRecord::Base
  set_primary_key "sysid"
end


48
49
50
51
# File 'lib/active_record/attribute_methods/primary_key.rb', line 48

def set_primary_key(value = nil, &block)
  @quoted_primary_key = nil
  define_attr_method :primary_key, value, &block
end