You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -93,9 +93,9 @@ The primary operation of `Model.find(options)` can be summarized as:
93
93
94
94
Active Record provides several different ways of retrieving a single object.
95
95
96
-
#### Using a Primary Key
96
+
#### `find`
97
97
98
-
Using `Model.find(primary_key)`, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example:
98
+
Using the `find` method, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example:
99
99
100
100
```ruby
101
101
# Find the client with primary key (id) 10.
@@ -109,215 +109,180 @@ The SQL equivalent of the above is:
109
109
SELECT*FROM clients WHERE (clients.id=10) LIMIT1
110
110
```
111
111
112
-
`Model.find(primary_key)` will raise an `ActiveRecord::RecordNotFound` exception if no matching record is found.
112
+
The `find` method will raise an `ActiveRecord::RecordNotFound` exception if no matching record is found.
113
113
114
-
#### `take`
115
-
116
-
`Model.take` retrieves a record without any implicit ordering. For example:
114
+
You can also use this method to query for multiple objects. Call the `find` method and pass in an array of primary keys. The return will be an array containing all of the matching records for the supplied _primary keys_. For example:
117
115
118
116
```ruby
119
-
client =Client.take
120
-
# => #<Client id: 1, first_name: "Lifo">
117
+
# Find the clients with primary keys 1 and 10.
118
+
client =Client.find([1, 10]) # Or even Client.find(1, 10)
`Model.take` returns `nil` if no record is found and no exception will be raised.
130
-
131
-
TIP: The retrieved record may vary depending on the database engine.
128
+
WARNING: The `find` method will raise an `ActiveRecord::RecordNotFound` exception unless a matching record is found for **all** of the supplied primary keys.
132
129
133
-
#### `first`
130
+
#### `take`
134
131
135
-
`Model.first` finds the first record ordered by the primary key. For example:
132
+
The `take` method retrieves a record without any implicit ordering. For example:
136
133
137
134
```ruby
138
-
client =Client.first
135
+
client =Client.take
139
136
# => #<Client id: 1, first_name: "Lifo">
140
137
```
141
138
142
139
The SQL equivalent of the above is:
143
140
144
141
```sql
145
-
SELECT*FROM clients ORDER BYclients.idASCLIMIT1
142
+
SELECT*FROM clients LIMIT1
146
143
```
147
144
148
-
`Model.first` returns `nil` if no matching record is found and no exception will be raised.
149
-
150
-
#### `last`
145
+
The `take` method returns `nil` if no record is found and no exception will be raised.
151
146
152
-
`Model.last` finds the last record ordered by the primary key. For example:
147
+
You can pass in a numerical argument to the `take` method to return up to that number of results. For example
153
148
154
149
```ruby
155
-
client =Client.last
156
-
# => #<Client id: 221, first_name: "Russel">
150
+
client =Client.take(2)
151
+
# => [
152
+
#<Client id: 1, first_name: "Lifo">,
153
+
#<Client id: 220, first_name: "Sara">
154
+
]
157
155
```
158
156
159
157
The SQL equivalent of the above is:
160
158
161
159
```sql
162
-
SELECT*FROM clients ORDER BYclients.idDESCLIMIT1
160
+
SELECT*FROM clients LIMIT2
163
161
```
164
162
165
-
`Model.last` returns `nil` if no matching record is found and no exception will be raised.
163
+
The `take!` method behaves exactly like `take`, except that it will raise `ActiveRecord::RecordNotFound` if no matching record is found.
166
164
167
-
#### `find_by`
165
+
TIP: The retrieved record may vary depending on the database engine.
168
166
169
-
`Model.find_by` finds the first record matching some conditions. For example:
167
+
#### `first`
168
+
169
+
The `first` method finds the first record ordered by the primary key. For example:
170
170
171
171
```ruby
172
-
Client.find_by first_name:'Lifo'
172
+
client =Client.first
173
173
# => #<Client id: 1, first_name: "Lifo">
174
-
175
-
Client.find_by first_name:'Jon'
176
-
# => nil
177
174
```
178
175
179
-
It is equivalent to writing:
176
+
The SQL equivalent of the above is:
180
177
181
-
```ruby
182
-
Client.where(first_name:'Lifo').take
178
+
```sql
179
+
SELECT*FROM clients ORDER BYclients.idASCLIMIT1
183
180
```
184
181
185
-
#### `take!`
182
+
The `first` method returns `nil` if no matching record is found and no exception will be raised.
186
183
187
-
`Model.take!` retrieves a record without any implicit ordering. For example:
184
+
You can pass in a numerical argument to the `first` method to return up to that number of results. For example
188
185
189
186
```ruby
190
-
client =Client.take!
191
-
# => #<Client id: 1, first_name: "Lifo">
187
+
client =Client.first(3)
188
+
# => [
189
+
#<Client id: 1, first_name: "Lifo">,
190
+
#<Client id: 2, first_name: "Fifo">,
191
+
#<Client id: 3, first_name: "Filo">
192
+
]
192
193
```
193
194
194
195
The SQL equivalent of the above is:
195
196
196
197
```sql
197
-
SELECT*FROM clients LIMIT1
198
+
SELECT*FROM clients ORDER BYclients.idASCLIMIT3
198
199
```
199
200
200
-
`Model.take!`raises`ActiveRecord::RecordNotFound` if no matching record is found.
201
+
The `first!`method behaves exactly like `first`, except that it will raise`ActiveRecord::RecordNotFound` if no matching record is found.
201
202
202
-
#### `first!`
203
+
#### `last`
203
204
204
-
`Model.first!`finds the first record ordered by the primary key. For example:
205
+
The `last` method finds the last record ordered by the primary key. For example:
205
206
206
207
```ruby
207
-
client =Client.first!
208
-
# => #<Client id: 1, first_name: "Lifo">
208
+
client =Client.last
209
+
# => #<Client id: 221, first_name: "Russel">
209
210
```
210
211
211
212
The SQL equivalent of the above is:
212
213
213
214
```sql
214
-
SELECT*FROM clients ORDER BYclients.idASCLIMIT1
215
+
SELECT*FROM clients ORDER BYclients.idDESCLIMIT1
215
216
```
216
217
217
-
`Model.first!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
218
-
219
-
#### `last!`
218
+
The `last` method returns `nil` if no matching record is found and no exception will be raised.
220
219
221
-
`Model.last!` finds the last record ordered by the primary key. For example:
220
+
You can pass in a numerical argument to the `last` method to return up to that number of results. For example
222
221
223
222
```ruby
224
-
client =Client.last!
225
-
# => #<Client id: 221, first_name: "Russel">
223
+
client =Client.last(3)
224
+
# => [
225
+
#<Client id: 219, first_name: "James">,
226
+
#<Client id: 220, first_name: "Sara">,
227
+
#<Client id: 221, first_name: "Russel">
228
+
]
226
229
```
227
230
228
231
The SQL equivalent of the above is:
229
232
230
233
```sql
231
-
SELECT*FROM clients ORDER BYclients.idDESCLIMIT1
234
+
SELECT*FROM clients ORDER BYclients.idDESCLIMIT3
232
235
```
233
236
234
-
`Model.last!`raises`ActiveRecord::RecordNotFound` if no matching record is found.
237
+
The `last!`method behaves exactly like `last`, except that it will raise`ActiveRecord::RecordNotFound` if no matching record is found.
235
238
236
-
#### `find_by!`
239
+
#### `find_by`
237
240
238
-
`Model.find_by!`finds the first record matching some conditions. It raises `ActiveRecord::RecordNotFound` if no matching record is found. For example:
241
+
The `find_by` method finds the first record matching some conditions. For example:
239
242
240
243
```ruby
241
-
Client.find_by!first_name:'Lifo'
244
+
Client.find_by first_name:'Lifo'
242
245
# => #<Client id: 1, first_name: "Lifo">
243
246
244
-
Client.find_by!first_name:'Jon'
245
-
# => ActiveRecord::RecordNotFound
247
+
Client.find_by first_name:'Jon'
248
+
# => nil
246
249
```
247
250
248
251
It is equivalent to writing:
249
252
250
253
```ruby
251
-
Client.where(first_name:'Lifo').take!
254
+
Client.where(first_name:'Lifo').take
252
255
```
253
256
254
-
### Retrieving Multiple Objects
255
-
256
-
#### Using Multiple Primary Keys
257
-
258
-
`Model.find(array_of_primary_key)` accepts an array of _primary keys_, returning an array containing all of the matching records for the supplied _primary keys_. For example:
257
+
The `find_by!` method behaves exactly like `find_by`, except that it will raise `ActiveRecord::RecordNotFound` if no matching record is found. For example:
259
258
260
259
```ruby
261
-
# Find the clients with primary keys 1 and 10.
262
-
client =Client.find([1, 10]) # Or even Client.find(1, 10)
WARNING: `Model.find(array_of_primary_key)` will raise an `ActiveRecord::RecordNotFound` exception unless a matching record is found for **all** of the supplied primary keys.
273
-
274
-
#### take
275
-
276
-
`Model.take(limit)` retrieves the first number of records specified by `limit` without any explicit ordering:
264
+
This is equivalent to writing:
277
265
278
266
```ruby
279
-
Client.take(2)
280
-
# => [#<Client id: 1, first_name: "Lifo">,
281
-
#<Client id: 2, first_name: "Raf">]
282
-
```
283
-
284
-
The SQL equivalent of the above is:
285
-
286
-
```sql
287
-
SELECT*FROM clients LIMIT2
267
+
Client.where(first_name:'does not exist').take!
288
268
```
289
269
290
-
#### first
270
+
#### `last!`
291
271
292
-
`Model.first(limit)` finds the first number of records specified by `limit`ordered by primary key:
272
+
`Model.last!` finds the last record ordered by the primary key. For example:
293
273
294
274
```ruby
295
-
Client.first(2)
296
-
# => [#<Client id: 1, first_name: "Lifo">,
297
-
#<Client id: 2, first_name: "Raf">]
275
+
client =Client.last!
276
+
# => #<Client id: 221, first_name: "Russel">
298
277
```
299
278
300
279
The SQL equivalent of the above is:
301
280
302
281
```sql
303
-
SELECT*FROM clients ORDER BY id ASCLIMIT2
304
-
```
305
-
306
-
#### last
307
-
308
-
`Model.last(limit)` finds the number of records specified by `limit` ordered by primary key in descending order:
309
-
310
-
```ruby
311
-
Client.last(2)
312
-
# => [#<Client id: 10, first_name: "Ryan">,
313
-
#<Client id: 9, first_name: "John">]
282
+
SELECT*FROM clients ORDER BYclients.idDESCLIMIT1
314
283
```
315
284
316
-
The SQL equivalent of the above is:
317
-
318
-
```sql
319
-
SELECT*FROM clients ORDER BY id DESCLIMIT2
320
-
```
285
+
`Model.last!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
321
286
322
287
### Retrieving Multiple Objects in Batches
323
288
@@ -344,7 +309,15 @@ The `find_each` method retrieves a batch of records and then yields _each_ recor
344
309
345
310
```ruby
346
311
User.find_each do |user|
347
-
NewsLetter.weekly_deliver(user)
312
+
NewsMailer.weekly(user).deliver
313
+
end
314
+
```
315
+
316
+
To add conditions to a `find_each` operation you can chain other Active Record methods such as `where`:
317
+
318
+
```ruby
319
+
User.where(weekly_subscriber:true).find_each do |user|
320
+
NewsMailer.weekly(user).deliver
348
321
end
349
322
```
350
323
@@ -707,7 +680,7 @@ Overriding Conditions
707
680
You can specify certain conditions to be removed using the `unscope` method. For example:
@@ -1487,6 +1458,11 @@ If you'd like to use your own SQL to find records in a table you can use `find_b
1487
1458
Client.find_by_sql("SELECT * FROM clients
1488
1459
INNER JOIN orders ON clients.id = orders.client_id
1489
1460
ORDER BY clients.created_at desc")
1461
+
# => [
1462
+
#<Client id: 1, first_name: "Lucas" >,
1463
+
#<Client id: 2, first_name: "Jan" >,
1464
+
# ...
1465
+
]
1490
1466
```
1491
1467
1492
1468
`find_by_sql` provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
@@ -1496,7 +1472,11 @@ Client.find_by_sql("SELECT * FROM clients
1496
1472
`find_by_sql` has a close relative called `connection#select_all`. `select_all` will retrieve objects from the database using custom SQL just like `find_by_sql` but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.
1497
1473
1498
1474
```ruby
1499
-
Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")
1475
+
Client.connection.select_all("SELECT first_name, created_at FROM clients WHERE id = '1'")
and you're running into one of them now. This one is called [strong parameters](http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters),
754
753
which requires us to tell Rails exactly which parameters are allowed into our
0 commit comments