Have you ever struggled with running one background job after another in Rails? What if you need to generate a report first, then email it to users? Or import a CSV file and notify users only when it's done?
These are common problems in Rails applications. Until now, developers had to write complex code to handle job dependencies. But Rails 7.1 introduced a game-changing feature: Rails Active Job continuations.
This feature makes chaining background jobs in Rails incredibly simple. Instead of writing messy callback code, you can now chain jobs with a single .then() method. Let's explore how this works and why it's so powerful.
Rails Active Job continuations let you chain jobs together in a clean, readable way. Here's the basic syntax:
ruby
MyFirstJob.perform_later(args).then(MySecondJob)
That's it! The second job will only run after the first one completes successfully. This feature is part of Rails 7.1 and makes job orchestration much simpler.
You should consider Rails job dependencies when you have:
The Active Job then method solves these problems elegantly.
Let's build a real example step by step.
First, generate two jobs using Rails generators:
Now comes the magic part:
That's all you need! The first job generates the report, and the second job emails it automatically.
Here's a crucial point: Rails Active Job continuations pass the return value from the first job to the second job automatically.
In our example:
This makes the data flow simple and predictable:
Important: Always return data that can be serialized (like numbers, strings, or simple hashes).
What happens if something goes wrong? Rails Active Job continuations have smart error handling:
You can configure retries like this:
Here's a practical example many developers face:
ImportCsvJob.perform_later(current_user.id, upload_path).then(NotifyUserJob)
Clean, simple, and reliable. The user only gets notified if the import succeeds.
Before Rails 7.1, developers used these approaches:
Problems:
Complex workflow gems like Railway or Trailblazer work well for:
But for simple job chaining, Rails Active Job continuations are much cleaner.
Testing chained jobs is straightforward:
Most of the time, you'll test the end result (like checking if an email was sent).
You can chain more than two jobs:
Each job passes its return value to the next one in line.
Need to pass multiple values? Return a hash:
Q: Do continuations work with Sidekiq or other job backends?
Yes! Since continuations are part of ActiveJob, they work with any supported backend.
Q: Can I pass multiple arguments to the second job?
No. The continuation receives one argument: the return value from the first job. Use a hash for multiple values.
Q: What if the second job fails?
It behaves like any normal job. Your retry and error handling work as expected. The first job isn't affected.
Q: Can I use this in older Rails versions?
No, you need Rails 7.1 or newer. For older versions, you'll need manual solutions.
Rails Active Job continuations transform how we handle chaining background jobs in Rails. The Active Job then method provides a clean, reliable way to manage Rails job dependencies without complex orchestration code. This feature makes your workflows more maintainable and easier to understand.
Ready to modernize your Rails application with better job management? Techdots can help you implement these advanced Rails features and optimize your background job workflows. Contact us today!
Techdots has helped 15+ founders transform their visions into market-ready AI products. Each started exactly where you are now - with an idea and the courage to act on it.
Techdots: Where Founder Vision Meets AI Reality
Book Meeting