rails 4.2.0.beta1 → 4.2.0.beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/guides/rails_guides/markdown.rb +6 -1
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +1 -1
- data/guides/source/3_0_release_notes.md +2 -2
- data/guides/source/3_1_release_notes.md +4 -1
- data/guides/source/3_2_release_notes.md +4 -1
- data/guides/source/4_0_release_notes.md +4 -1
- data/guides/source/4_1_release_notes.md +4 -4
- data/guides/source/4_2_release_notes.md +272 -45
- data/guides/source/action_controller_overview.md +3 -3
- data/guides/source/action_mailer_basics.md +28 -4
- data/guides/source/action_view_overview.md +11 -11
- data/guides/source/active_job_basics.md +99 -63
- data/guides/source/active_record_postgresql.md +2 -1
- data/guides/source/active_record_querying.md +7 -9
- data/guides/source/active_support_core_extensions.md +48 -2
- data/guides/source/asset_pipeline.md +200 -18
- data/guides/source/association_basics.md +8 -8
- data/guides/source/caching_with_rails.md +1 -1
- data/guides/source/command_line.md +1 -1
- data/guides/source/configuring.md +7 -7
- data/guides/source/contributing_to_ruby_on_rails.md +17 -0
- data/guides/source/debugging_rails_applications.md +2 -2
- data/guides/source/development_dependencies_install.md +41 -43
- data/guides/source/form_helpers.md +17 -17
- data/guides/source/generators.md +6 -2
- data/guides/source/getting_started.md +1 -1
- data/guides/source/i18n.md +16 -0
- data/guides/source/initialization.md +0 -2
- data/guides/source/layouts_and_rendering.md +2 -1
- data/guides/source/maintenance_policy.md +6 -3
- data/guides/source/nested_model_forms.md +4 -1
- data/guides/source/routing.md +1 -1
- data/guides/source/testing.md +2 -2
- data/guides/source/upgrading_ruby_on_rails.md +94 -23
- metadata +18 -18
@@ -1183,9 +1183,9 @@ First define your app own routes to display the errors page.
|
|
1183
1183
|
* `config/routes.rb`
|
1184
1184
|
|
1185
1185
|
```ruby
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1186
|
+
match '/404', via: :all, to: 'errors#not_found'
|
1187
|
+
match '/422', via: :all, to: 'errors#unprocessable_entity'
|
1188
|
+
match '/500', via: :all, to: 'errors#server_error'
|
1189
1189
|
```
|
1190
1190
|
|
1191
1191
|
Create the controller and views.
|
@@ -159,7 +159,10 @@ $ bin/rake db:migrate
|
|
159
159
|
Now that we have a user model to play with, we will just edit the
|
160
160
|
`app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
|
161
161
|
an email to the newly created user by editing the create action and inserting a
|
162
|
-
call to `UserMailer.welcome_email` right after the user is successfully saved
|
162
|
+
call to `UserMailer.welcome_email` right after the user is successfully saved.
|
163
|
+
|
164
|
+
Action Mailer is nicely integrated with Active Job so you can send emails outside
|
165
|
+
of the request-response cycle, so the user doesn't have to wait on it:
|
163
166
|
|
164
167
|
```ruby
|
165
168
|
class UsersController < ApplicationController
|
@@ -171,7 +174,7 @@ class UsersController < ApplicationController
|
|
171
174
|
respond_to do |format|
|
172
175
|
if @user.save
|
173
176
|
# Tell the UserMailer to send a welcome email after save
|
174
|
-
UserMailer.welcome_email(@user).
|
177
|
+
UserMailer.welcome_email(@user).deliver_later
|
175
178
|
|
176
179
|
format.html { redirect_to(@user, notice: 'User was successfully created.') }
|
177
180
|
format.json { render json: @user, status: :created, location: @user }
|
@@ -184,8 +187,29 @@ class UsersController < ApplicationController
|
|
184
187
|
end
|
185
188
|
```
|
186
189
|
|
187
|
-
|
188
|
-
|
190
|
+
NOTE: Active Job's default behavior is to execute jobs ':inline'. So, you can use
|
191
|
+
`deliver_later` now to send emails, and when you later decide to start sending
|
192
|
+
them from a background job, you'll only need to set up Active Job to use a queueing
|
193
|
+
backend (Sidekiq, Resque, etc).
|
194
|
+
|
195
|
+
If you want to send emails right away (from a cronjob for example) just call
|
196
|
+
`deliver_now`:
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
class SendWeeklySummary
|
200
|
+
def run
|
201
|
+
User.find_each do |user|
|
202
|
+
UserMailer.weekly_summary(user).deliver_now
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
```
|
207
|
+
|
208
|
+
The method `welcome_email` returns a `ActionMailer::MessageDelivery` object which
|
209
|
+
can then just be told `deliver_now` or `deliver_later` to send itself out. The
|
210
|
+
`ActionMailer::MessageDelivery` object is just a wrapper around a `Mail::Message`. If
|
211
|
+
you want to inspect, alter or do anything else with the `Mail::Message` object you can
|
212
|
+
access it with the `message` method on the `ActionMailer::MessageDelivery` object.
|
189
213
|
|
190
214
|
### Auto encoding header values
|
191
215
|
|
@@ -44,18 +44,18 @@ $ bin/rails generate scaffold article
|
|
44
44
|
|
45
45
|
There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above.
|
46
46
|
For example, the index controller action of the `articles_controller.rb` will use the `index.html.erb` view file in the `app/views/articles` directory.
|
47
|
-
The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference.
|
47
|
+
The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference. Within this guide you will find more detailed documentation about each of these three components.
|
48
48
|
|
49
49
|
|
50
50
|
Templates, Partials and Layouts
|
51
51
|
-------------------------------
|
52
52
|
|
53
|
-
As mentioned
|
54
|
-
Below is a brief overview of each
|
53
|
+
As mentioned, the final HTML output is a composition of three Rails elements: `Templates`, `Partials` and `Layouts`.
|
54
|
+
Below is a brief overview of each of them.
|
55
55
|
|
56
56
|
### Templates
|
57
57
|
|
58
|
-
Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (
|
58
|
+
Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (Embedded Ruby) and HTML. If the template file has a `.builder` extension then the `Builder::XmlMarkup` library is used.
|
59
59
|
|
60
60
|
Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an HTML file using the ERB template system will have `.html.erb` as a file extension.
|
61
61
|
|
@@ -72,7 +72,7 @@ Consider the following loop for names:
|
|
72
72
|
<% end %>
|
73
73
|
```
|
74
74
|
|
75
|
-
The loop is set up
|
75
|
+
The loop is set up using regular embedding tags (`<% %>`) and the name is inserted using the output embedding tags (`<%= %>`). Note that this is not just a usage suggestion: regular output functions such as `print` and `puts` won't be rendered to the view with ERB templates. So this would be wrong:
|
76
76
|
|
77
77
|
```html+erb
|
78
78
|
<%# WRONG %>
|
@@ -231,7 +231,7 @@ The `object` and `as` options can also be used together:
|
|
231
231
|
|
232
232
|
#### Rendering Collections
|
233
233
|
|
234
|
-
It is very common that a template
|
234
|
+
It is very common that a template will need to iterate over a collection and render a sub-template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial for each one of the elements in the array.
|
235
235
|
|
236
236
|
So this example for rendering all the products:
|
237
237
|
|
@@ -247,7 +247,7 @@ can be rewritten in a single line:
|
|
247
247
|
<%= render partial: "product", collection: @products %>
|
248
248
|
```
|
249
249
|
|
250
|
-
When a partial is called
|
250
|
+
When a partial is called with a collection, the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is `_product`, and within it you can refer to `product` to get the collection member that is being rendered.
|
251
251
|
|
252
252
|
You can use a shorthand syntax for rendering collections. Assuming `@products` is a collection of `Product` instances, you can simply write the following to produce the same result:
|
253
253
|
|
@@ -255,7 +255,7 @@ You can use a shorthand syntax for rendering collections. Assuming `@products` i
|
|
255
255
|
<%= render @products %>
|
256
256
|
```
|
257
257
|
|
258
|
-
Rails determines the name of the partial to use by looking at the model name in the collection, `Product` in this case. In fact, you can even
|
258
|
+
Rails determines the name of the partial to use by looking at the model name in the collection, `Product` in this case. In fact, you can even render a collection made up of instances of different models using this shorthand, and Rails will choose the proper partial for each member of the collection.
|
259
259
|
|
260
260
|
#### Spacer Templates
|
261
261
|
|
@@ -269,14 +269,14 @@ Rails will render the `_product_ruler` partial (with no data passed to it) betwe
|
|
269
269
|
|
270
270
|
### Layouts
|
271
271
|
|
272
|
-
Layouts can be used to render a common view template around the results of Rails controller actions. Typically,
|
272
|
+
Layouts can be used to render a common view template around the results of Rails controller actions. Typically, a Rails application will have a couple of layouts that pages will be rendered within. For example, a site might have one layout for a logged in user and another for the marketing or sales side of the site. The logged in user layout might include top-level navigation that should be present across many controller actions. The sales layout for a SaaS app might include top-level navigation for things like "Pricing" and "Contact Us" pages. You would expect each layout to have a different look and feel. You can read about layouts in more detail in the [Layouts and Rendering in Rails](layouts_and_rendering.html) guide.
|
273
273
|
|
274
274
|
Partial Layouts
|
275
275
|
---------------
|
276
276
|
|
277
|
-
Partials can have their own layouts applied to them. These layouts are different
|
277
|
+
Partials can have their own layouts applied to them. These layouts are different from those applied to a controller action, but they work in a similar fashion.
|
278
278
|
|
279
|
-
Let's say we're displaying an article on a page
|
279
|
+
Let's say we're displaying an article on a page which should be wrapped in a `div` for display purposes. Firstly, we'll create a new `Article`:
|
280
280
|
|
281
281
|
```ruby
|
282
282
|
Article.create(body: 'Partial Layouts are cool!')
|
@@ -13,12 +13,13 @@ After reading this guide, you will know:
|
|
13
13
|
|
14
14
|
--------------------------------------------------------------------------------
|
15
15
|
|
16
|
+
|
16
17
|
Introduction
|
17
18
|
------------
|
18
19
|
|
19
20
|
Active Job is a framework for declaring jobs and making them run on a variety
|
20
21
|
of queueing backends. These jobs can be everything from regularly scheduled
|
21
|
-
clean-ups, billing charges,
|
22
|
+
clean-ups, to billing charges, to mailings. Anything that can be chopped up
|
22
23
|
into small units of work and run in parallel, really.
|
23
24
|
|
24
25
|
|
@@ -35,12 +36,12 @@ then. And you'll be able to switch between them without having to rewrite your j
|
|
35
36
|
Creating a Job
|
36
37
|
--------------
|
37
38
|
|
38
|
-
This section will provide a step-by-step guide to creating a job and
|
39
|
+
This section will provide a step-by-step guide to creating a job and enqueuing it.
|
39
40
|
|
40
41
|
### Create the Job
|
41
42
|
|
42
43
|
Active Job provides a Rails generator to create jobs. The following will create a
|
43
|
-
job in app/jobs
|
44
|
+
job in `app/jobs`:
|
44
45
|
|
45
46
|
```bash
|
46
47
|
$ bin/rails generate job guests_cleanup
|
@@ -58,15 +59,15 @@ As you can see, you can generate jobs just like you use other generators with
|
|
58
59
|
Rails.
|
59
60
|
|
60
61
|
If you don't want to use a generator, you could create your own file inside of
|
61
|
-
app/jobs
|
62
|
+
`app/jobs`, just make sure that it inherits from `ActiveJob::Base`.
|
62
63
|
|
63
|
-
Here's
|
64
|
+
Here's what a job looks like:
|
64
65
|
|
65
66
|
```ruby
|
66
67
|
class GuestsCleanupJob < ActiveJob::Base
|
67
68
|
queue_as :default
|
68
69
|
|
69
|
-
def perform
|
70
|
+
def perform(*args)
|
70
71
|
# Do something later
|
71
72
|
end
|
72
73
|
end
|
@@ -77,15 +78,15 @@ end
|
|
77
78
|
Enqueue a job like so:
|
78
79
|
|
79
80
|
```ruby
|
80
|
-
MyJob.
|
81
|
+
MyJob.perform_later record # Enqueue a job to be performed as soon the queueing system is free.
|
81
82
|
```
|
82
83
|
|
83
84
|
```ruby
|
84
|
-
MyJob.
|
85
|
+
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record) # Enqueue a job to be performed tomorrow at noon.
|
85
86
|
```
|
86
87
|
|
87
88
|
```ruby
|
88
|
-
MyJob.
|
89
|
+
MyJob.set(wait: 1.week).perform_later(record) # Enqueue a job to be performed 1 week from now.
|
89
90
|
```
|
90
91
|
|
91
92
|
That's it!
|
@@ -94,52 +95,30 @@ That's it!
|
|
94
95
|
Job Execution
|
95
96
|
-------------
|
96
97
|
|
97
|
-
If
|
98
|
+
If no adapter is set, the job is immediately executed.
|
98
99
|
|
99
100
|
### Backends
|
100
101
|
|
101
|
-
Active Job has adapters for
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
* [Resque 1.x](https://github.com/resque/resque)
|
109
|
-
* [Sidekiq](https://github.com/mperham/sidekiq)
|
110
|
-
* [Sneakers](https://github.com/jondot/sneakers)
|
111
|
-
* [Sucker Punch](https://github.com/brandonhilkert/sucker_punch)
|
112
|
-
|
113
|
-
#### Backends Features
|
114
|
-
|
115
|
-
| | Async | Queues | Delayed | Priorities | Timeout | Retries |
|
116
|
-
|-----------------------|-------|---------|---------|-------------|---------|---------|
|
117
|
-
| **Backburner** | Yes | Yes | Yes | Yes | Job | Global |
|
118
|
-
| **Delayed Job** | Yes | Yes | Yes | Job | Global | Global |
|
119
|
-
| **Que** | Yes | Yes | Yes | Job | No | Job |
|
120
|
-
| **Queue Classic** | Yes | Yes | Gem | No | No | No |
|
121
|
-
| **Resque** | Yes | Yes | Gem | Queue | Global | ? |
|
122
|
-
| **Sidekiq** | Yes | Yes | Yes | Queue | No | Job |
|
123
|
-
| **Sneakers** | Yes | Yes | No | Queue | Queue | No |
|
124
|
-
| **Sucker Punch** | Yes | Yes | Yes | No | No | No |
|
125
|
-
| **Active Job** | Yes | Yes | WIP | No | No | No |
|
126
|
-
| **Active Job Inline** | No | Yes | N/A | N/A | N/A | N/A |
|
127
|
-
|
128
|
-
### Change Backends
|
129
|
-
|
130
|
-
You can easy change your adapter:
|
102
|
+
Active Job has built-in adapters for multiple queueing backends (Sidekiq,
|
103
|
+
Resque, Delayed Job and others). To get an up-to-date list of the adapters
|
104
|
+
see the API Documentation for [ActiveJob::QueueAdapters](http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
|
105
|
+
|
106
|
+
### Changing the Backend
|
107
|
+
|
108
|
+
You can easily change your queueing backend:
|
131
109
|
|
132
110
|
```ruby
|
133
111
|
# be sure to have the adapter gem in your Gemfile and follow the adapter specific
|
134
112
|
# installation and deployment instructions
|
135
|
-
|
113
|
+
Rails.application.config.active_job.queue_adapter = :sidekiq
|
136
114
|
```
|
137
115
|
|
116
|
+
|
138
117
|
Queues
|
139
118
|
------
|
140
119
|
|
141
|
-
Most of the adapters
|
142
|
-
to run on a specific queue:
|
120
|
+
Most of the adapters support multiple queues. With Active Job you can schedule
|
121
|
+
the job to run on a specific queue:
|
143
122
|
|
144
123
|
```ruby
|
145
124
|
class GuestsCleanupJob < ActiveJob::Base
|
@@ -148,24 +127,77 @@ class GuestsCleanupJob < ActiveJob::Base
|
|
148
127
|
end
|
149
128
|
```
|
150
129
|
|
151
|
-
|
152
|
-
|
130
|
+
You can prefix the queue name for all your jobs using
|
131
|
+
`config.active_job.queue_name_prefix` in `application.rb`:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# config/application.rb
|
135
|
+
module YourApp
|
136
|
+
class Application < Rails::Application
|
137
|
+
config.active_job.queue_name_prefix = Rails.env
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# app/jobs/guests_cleanup.rb
|
142
|
+
class GuestsCleanupJob < ActiveJob::Base
|
143
|
+
queue_as :low_priority
|
144
|
+
#....
|
145
|
+
end
|
146
|
+
|
147
|
+
# Now your job will run on queue production_low_priority on your
|
148
|
+
# production environment and on beta_low_priority on your beta
|
149
|
+
# environment
|
150
|
+
```
|
151
|
+
|
152
|
+
If you want more control on what queue a job will be run you can pass a :queue
|
153
|
+
option to #set:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
MyJob.set(queue: :another_queue).perform_later(record)
|
157
|
+
```
|
158
|
+
|
159
|
+
To control the queue from the job level you can pass a block to queue_as. The
|
160
|
+
block will be executed in the job context (so you can access self.arguments)
|
161
|
+
and you must return the queue name:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
class ProcessVideoJob < ActiveJob::Base
|
165
|
+
queue_as do
|
166
|
+
video = self.arguments.first
|
167
|
+
if video.owner.premium?
|
168
|
+
:premium_videojobs
|
169
|
+
else
|
170
|
+
:videojobs
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def perform(video)
|
175
|
+
# do process video
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
ProcessVideoJob.perform_later(Video.last)
|
180
|
+
```
|
181
|
+
|
182
|
+
|
183
|
+
NOTE: Make sure your queueing backend "listens" on your queue name. For some
|
184
|
+
backends you need to specify the queues to listen to.
|
153
185
|
|
154
186
|
|
155
187
|
Callbacks
|
156
188
|
---------
|
157
189
|
|
158
|
-
Active Job provides hooks during the lifecycle of a job. Callbacks
|
159
|
-
logic during the lifecycle of a job.
|
190
|
+
Active Job provides hooks during the lifecycle of a job. Callbacks allow you to
|
191
|
+
trigger logic during the lifecycle of a job.
|
160
192
|
|
161
193
|
### Available callbacks
|
162
194
|
|
163
|
-
* before_enqueue
|
164
|
-
* around_enqueue
|
165
|
-
* after_enqueue
|
166
|
-
* before_perform
|
167
|
-
* around_perform
|
168
|
-
* after_perform
|
195
|
+
* `before_enqueue`
|
196
|
+
* `around_enqueue`
|
197
|
+
* `after_enqueue`
|
198
|
+
* `before_perform`
|
199
|
+
* `around_perform`
|
200
|
+
* `after_perform`
|
169
201
|
|
170
202
|
### Usage
|
171
203
|
|
@@ -174,7 +206,7 @@ class GuestsCleanupJob < ActiveJob::Base
|
|
174
206
|
queue_as :default
|
175
207
|
|
176
208
|
before_enqueue do |job|
|
177
|
-
# do
|
209
|
+
# do something with the job instance
|
178
210
|
end
|
179
211
|
|
180
212
|
around_perform do |job, block|
|
@@ -189,25 +221,28 @@ class GuestsCleanupJob < ActiveJob::Base
|
|
189
221
|
end
|
190
222
|
```
|
191
223
|
|
224
|
+
|
192
225
|
ActionMailer
|
193
226
|
------------
|
227
|
+
|
194
228
|
One of the most common jobs in a modern web application is sending emails outside
|
195
229
|
of the request-response cycle, so the user doesn't have to wait on it. Active Job
|
196
|
-
is integrated with Action Mailer so you can easily send emails
|
230
|
+
is integrated with Action Mailer so you can easily send emails asynchronously:
|
197
231
|
|
198
232
|
```ruby
|
199
|
-
#
|
200
|
-
UserMailer.welcome(@user).
|
233
|
+
# If you want to send the email now use #deliver_now
|
234
|
+
UserMailer.welcome(@user).deliver_now
|
201
235
|
|
202
|
-
#
|
236
|
+
# If you want to send the email through Active Job use #deliver_later
|
203
237
|
UserMailer.welcome(@user).deliver_later
|
204
238
|
```
|
205
239
|
|
240
|
+
|
206
241
|
GlobalID
|
207
242
|
--------
|
208
|
-
Active Job supports GlobalID for parameters. This makes it possible
|
209
|
-
|
210
|
-
|
243
|
+
Active Job supports GlobalID for parameters. This makes it possible to pass live
|
244
|
+
Active Record objects to your job instead of class/id pairs, which you then have
|
245
|
+
to manually deserialize. Before, jobs would look like this:
|
211
246
|
|
212
247
|
```ruby
|
213
248
|
class TrashableCleanupJob
|
@@ -228,12 +263,13 @@ class TrashableCleanupJob
|
|
228
263
|
end
|
229
264
|
```
|
230
265
|
|
231
|
-
This works with any class that mixes in ActiveModel::GlobalIdentification
|
266
|
+
This works with any class that mixes in `ActiveModel::GlobalIdentification`, which
|
232
267
|
by default has been mixed into Active Model classes.
|
233
268
|
|
234
269
|
|
235
270
|
Exceptions
|
236
271
|
----------
|
272
|
+
|
237
273
|
Active Job provides a way to catch exceptions raised during the execution of the
|
238
274
|
job:
|
239
275
|
|
@@ -242,7 +278,7 @@ job:
|
|
242
278
|
class GuestsCleanupJob < ActiveJob::Base
|
243
279
|
queue_as :default
|
244
280
|
|
245
|
-
rescue_from(ActiveRecord
|
281
|
+
rescue_from(ActiveRecord::RecordNotFound) do |exception|
|
246
282
|
# do something with the exception
|
247
283
|
end
|
248
284
|
|
@@ -132,7 +132,8 @@ event = Event.first
|
|
132
132
|
event.payload # => {"kind"=>"user_renamed", "change"=>["jack", "john"]}
|
133
133
|
|
134
134
|
## Query based on JSON document
|
135
|
-
|
135
|
+
# The -> operator returns the original JSON type (which might be an object), whereas ->> returns text
|
136
|
+
Event.where("payload->>'kind' = ?", "user_renamed")
|
136
137
|
```
|
137
138
|
|
138
139
|
### Range Types
|
@@ -276,7 +276,7 @@ This may appear straightforward:
|
|
276
276
|
```ruby
|
277
277
|
# This is very inefficient when the users table has thousands of rows.
|
278
278
|
User.all.each do |user|
|
279
|
-
NewsMailer.weekly(user).
|
279
|
+
NewsMailer.weekly(user).deliver_now
|
280
280
|
end
|
281
281
|
```
|
282
282
|
|
@@ -292,7 +292,7 @@ The `find_each` method retrieves a batch of records and then yields _each_ recor
|
|
292
292
|
|
293
293
|
```ruby
|
294
294
|
User.find_each do |user|
|
295
|
-
NewsMailer.weekly(user).
|
295
|
+
NewsMailer.weekly(user).deliver_now
|
296
296
|
end
|
297
297
|
```
|
298
298
|
|
@@ -300,7 +300,7 @@ To add conditions to a `find_each` operation you can chain other Active Record m
|
|
300
300
|
|
301
301
|
```ruby
|
302
302
|
User.where(weekly_subscriber: true).find_each do |user|
|
303
|
-
NewsMailer.weekly(user).
|
303
|
+
NewsMailer.weekly(user).deliver_now
|
304
304
|
end
|
305
305
|
```
|
306
306
|
|
@@ -316,7 +316,7 @@ The `:batch_size` option allows you to specify the number of records to be retri
|
|
316
316
|
|
317
317
|
```ruby
|
318
318
|
User.find_each(batch_size: 5000) do |user|
|
319
|
-
NewsMailer.weekly(user).
|
319
|
+
NewsMailer.weekly(user).deliver_now
|
320
320
|
end
|
321
321
|
```
|
322
322
|
|
@@ -328,7 +328,7 @@ For example, to send newsletters only to users with the primary key starting fro
|
|
328
328
|
|
329
329
|
```ruby
|
330
330
|
User.find_each(start: 2000, batch_size: 5000) do |user|
|
331
|
-
NewsMailer.weekly(user).
|
331
|
+
NewsMailer.weekly(user).deliver_now
|
332
332
|
end
|
333
333
|
```
|
334
334
|
|
@@ -340,16 +340,14 @@ The `find_in_batches` method is similar to `find_each`, since both retrieve batc
|
|
340
340
|
|
341
341
|
```ruby
|
342
342
|
# Give add_invoices an array of 1000 invoices at a time
|
343
|
-
Invoice.find_in_batches
|
343
|
+
Invoice.find_in_batches do |invoices|
|
344
344
|
export.add_invoices(invoices)
|
345
345
|
end
|
346
346
|
```
|
347
347
|
|
348
|
-
NOTE: The `:include` option allows you to name associations that should be loaded alongside with the models.
|
349
|
-
|
350
348
|
##### Options for `find_in_batches`
|
351
349
|
|
352
|
-
The `find_in_batches` method accepts the same `:batch_size` and `:start` options as `find_each
|
350
|
+
The `find_in_batches` method accepts the same `:batch_size` and `:start` options as `find_each`.
|
353
351
|
|
354
352
|
Conditions
|
355
353
|
----------
|