Skip to content

Commit 3bc6401

Browse files
authored
Merge pull request rails#49112 from gmcgibbon/cpk_id_docs
Composite Primary Key id method docs
2 parents 7c65a4b + 5ae1c5a commit 3bc6401

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

activerecord/lib/active_record/attribute_methods/primary_key.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def to_key
1515
Array(key) if key
1616
end
1717

18-
# Returns the primary key column's value.
18+
# Returns the primary key column's value. If the primary key is composite,
19+
# returns an array of the primary key column values.
1920
def id
2021
return _read_attribute(@primary_key) unless @primary_key.is_a?(Array)
2122

@@ -28,7 +29,8 @@ def primary_key_values_present? # :nodoc:
2829
!!id
2930
end
3031

31-
# Sets the primary key column's value.
32+
# Sets the primary key column's value. If the primary key is composite,
33+
# raises TypeError when the set value not enumerable.
3234
def id=(value)
3335
if self.class.composite_primary_key?
3436
raise TypeError, "Expected value matching #{self.class.primary_key.inspect}, got #{value.inspect}." unless value.is_a?(Enumerable)
@@ -38,7 +40,8 @@ def id=(value)
3840
end
3941
end
4042

41-
# Queries the primary key column's value.
43+
# Queries the primary key column's value. If the primary key is composite,
44+
# all primary key column values must be queryable.
4245
def id?
4346
if self.class.composite_primary_key?
4447
@primary_key.all? { |col| _query_attribute(col) }
@@ -47,7 +50,8 @@ def id?
4750
end
4851
end
4952

50-
# Returns the primary key column's value before type cast.
53+
# Returns the primary key column's value before type cast. If the primary key is composite,
54+
# returns an array of primary key column values before type cast.
5155
def id_before_type_cast
5256
if self.class.composite_primary_key?
5357
@primary_key.map { |col| attribute_before_type_cast(col) }
@@ -56,7 +60,8 @@ def id_before_type_cast
5660
end
5761
end
5862

59-
# Returns the primary key column's previous value.
63+
# Returns the primary key column's previous value. If the primary key is composite,
64+
# returns an array of primary key column previous values.
6065
def id_was
6166
if self.class.composite_primary_key?
6267
@primary_key.map { |col| attribute_was(col) }
@@ -65,7 +70,8 @@ def id_was
6570
end
6671
end
6772

68-
# Returns the primary key column's value from the database.
73+
# Returns the primary key column's value from the database. If the primary key is composite,
74+
# returns an array of primary key column values from database.
6975
def id_in_database
7076
if self.class.composite_primary_key?
7177
@primary_key.map { |col| attribute_in_database(col) }

activerecord/lib/active_record/model_schema.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ module ActiveRecord
66
module ModelSchema
77
extend ActiveSupport::Concern
88

9+
##
10+
# :method: id_value
11+
# :call-seq: id_valiue
12+
#
13+
# Returns the underlying column value for a column named "id". Useful when defining
14+
# a composite primary key including an "id" column so that the value is readable.
15+
916
##
1017
# :singleton-method: primary_key_prefix_type
1118
# :call-seq: primary_key_prefix_type

0 commit comments

Comments
 (0)