From 82a78bbd5c33d8a6a09a36992e21f15a5985251f Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Wed, 20 Nov 2013 11:48:58 -0500 Subject: [PATCH 1/2] Mailers section applies to Rails 4 too --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 135796846..cb11c2454 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,9 @@ end If you ever want to call a `handle_asynchronously`'d method without Delayed Job, for instance while debugging something at the console, just add `_without_delay` to the method name. For instance, if your original method was `foo`, then call `foo_without_delay`. -Rails 3 Mailers -=============== -Due to how mailers are implemented in Rails 3, we had to do a little work around to get delayed_job to work. +Mailers in Rails 3 and 4 +======================== +Due to how mailers are implemented in Rails 3 and 4, we had to do a little work around to get delayed_job to work. ```ruby # without delayed_job From 6e64695a056fff28b361563b298ef30824560023 Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Wed, 20 Nov 2013 11:51:23 -0500 Subject: [PATCH 2/2] Encourage using IDs for delayed methods --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cb11c2454..f4ac4ac30 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,11 @@ Call `.delay.method(params)` on any object and it will be processed in the backg @user.activate!(@device) # with delayed_job -@user.delay.activate!(@device) +@user.delay.activate!(@device.id) ``` +Note that we are passing in the device ID to the delayed version, and not the Device object itself. This is because by the time the job runs, the `@device` object may be out of sync with the actual Device record in the database. Therefore, we pass in the ID and call `device = Device.find(device_id)` in the `User#activate!` method, ensuring that we have an up-to-date version of the device when the job runs. + If a method should always be run in the background, you can call `#handle_asynchronously` after the method declaration: @@ -144,10 +146,10 @@ Due to how mailers are implemented in Rails 3 and 4, we had to do a little work Notifier.signup(@user).deliver # with delayed_job -Notifier.delay.signup(@user) +Notifier.delay.signup(@user.id) # with delayed_job running at a specific time -Notifier.delay(run_at: 5.minutes.from_now).signup(@user) +Notifier.delay(run_at: 5.minutes.from_now).signup(@user.id) ``` Remove the `.deliver` method to make it work. It's not ideal, but it's the best