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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#original_primary_keyObject

Returns the value of attribute original_primary_key.



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

def original_primary_key
  @original_primary_key
end

Instance Method Details

#get_primary_key(base_name) ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_record/attribute_methods/primary_key.rb', line 33

def get_primary_key(base_name) #:nodoc:
  return 'id' unless base_name && !base_name.blank?

  case primary_key_prefix_type
  when :table_name
    base_name.foreign_key(false)
  when :table_name_with_underscore
    base_name.foreign_key
  else
    if ActiveRecord::Base != self && connection.table_exists?(table_name)
      connection.primary_key(table_name)
    else
      'id'
    end
  end
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
18
# File 'lib/active_record/attribute_methods/primary_key.rb', line 15

def primary_key
  @primary_key = reset_primary_key unless defined? @primary_key
  @primary_key
end

#primary_key=(value) ⇒ Object

Attribute writer for the primary key column



53
54
55
56
57
58
# File 'lib/active_record/attribute_methods/primary_key.rb', line 53

def primary_key=(value)
  @quoted_primary_key = nil
  @primary_key = value

  connection_pool.primary_keys[table_name] = @primary_key if connected?
end

#quoted_primary_keyObject

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



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

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

#reset_primary_keyObject

:nodoc:



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

def reset_primary_key #:nodoc:
  key = self == base_class ? get_primary_key(base_class.name) :
    base_class.primary_key

  set_primary_key(key)
  key
end

#set_primary_key(value = nil, &block) ⇒ Object

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


67
68
69
70
71
72
73
# File 'lib/active_record/attribute_methods/primary_key.rb', line 67

def set_primary_key(value = nil, &block)
  @quoted_primary_key = nil
  @primary_key ||= ''
  self.original_primary_key = @primary_key
  value &&= value.to_s
  self.primary_key = block_given? ? instance_eval(&block) : value
end