@@ -6,7 +6,9 @@ module ActiveRecord
6
6
module FinderMethods
7
7
ONE_AS_ONE = "1 AS one"
8
8
9
- # Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]).
9
+ # Find by id - This can either be a specific id (ID), a list of ids (ID, ID, ID), or an array of ids ([ID, ID, ID]).
10
+ # `ID` refers to an "identifier". For models with a single-column primary key, `ID` will be a single value,
11
+ # and for models with a composite primary key, it will be an array of values.
10
12
# If one or more records cannot be found for the requested ids, then ActiveRecord::RecordNotFound will be raised.
11
13
# If the primary key is an integer, find by id coerces its arguments by using +to_i+.
12
14
#
@@ -18,6 +20,27 @@ module FinderMethods
18
20
# Person.find([1]) # returns an array for the object with ID = 1
19
21
# Person.where("administrator = 1").order("created_on DESC").find(1)
20
22
#
23
+ # ==== Find a record for a composite primary key model
24
+ # TravelRoute.primary_key = [:origin, :destination]
25
+ #
26
+ # TravelRoute.find(["Ottawa", "London"])
27
+ # => #<TravelRoute origin: "Ottawa", destination: "London">
28
+ #
29
+ # TravelRoute.find([["Paris", "Montreal"]])
30
+ # => [#<TravelRoute origin: "Paris", destination: "Montreal">]
31
+ #
32
+ # TravelRoute.find(["New York", "Las Vegas"], ["New York", "Portland"])
33
+ # => [
34
+ # #<TravelRoute origin: "New York", destination: "Las Vegas">,
35
+ # #<TravelRoute origin: "New York", destination: "Portland">
36
+ # ]
37
+ #
38
+ # TravelRoute.find([["Berlin", "London"], ["Barcelona", "Lisbon"]])
39
+ # => [
40
+ # #<TravelRoute origin: "Berlin", destination: "London">,
41
+ # #<TravelRoute origin: "Barcelona", destination: "Lisbon">
42
+ # ]
43
+ #
21
44
# NOTE: The returned records are in the same order as the ids you provide.
22
45
# If you want the results to be sorted by database, you can use ActiveRecord::QueryMethods#where
23
46
# method and provide an explicit ActiveRecord::QueryMethods#order option.
0 commit comments