Bird
Raised Fist0
Ruby on Railsframework~5 mins

Active Job framework in Ruby on Rails

Choose your learning style10 modes available
Introduction

Active Job helps you run tasks in the background so your app stays fast. It makes it easy to do things later without slowing down users.

Sending emails after a user signs up without making them wait
Processing image uploads or resizing photos after upload
Running slow database cleanup tasks at night
Sending notifications or messages without delay
Performing API calls to other services without blocking user actions
Syntax
Ruby on Rails
class MyJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do the work here
  end
end
Define a job by creating a class that inherits from ApplicationJob.
Use the perform method to write the task you want to run in the background.
Examples
This job sends a welcome email to a user using the mailers queue.
Ruby on Rails
class EmailUserJob < ApplicationJob
  queue_as :mailers

  def perform(user)
    UserMailer.welcome_email(user).deliver_now
  end
end
This job deletes old records using a low priority queue.
Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :low_priority

  def perform
    OldRecord.where('created_at < ?', 1.month.ago).delete_all
  end
end
Enqueue the job to run later with arguments.
Ruby on Rails
MyJob.perform_later(arg1, arg2)
Sample Program

This job prints a greeting message with the given name. It runs in the background when enqueued.

Ruby on Rails
class GreetUserJob < ApplicationJob
  queue_as :default

  def perform(name)
    puts "Hello, #{name}!"
  end
end

# Enqueue the job
GreetUserJob.perform_later('Alice')
OutputSuccess
Important Notes

Active Job works with many background systems like Sidekiq, Resque, or Delayed Job.

Use perform_later to run jobs asynchronously and perform_now to run immediately.

Always keep jobs small and focused to avoid long delays.

Summary

Active Job lets you run tasks in the background easily.

Define jobs by subclassing ApplicationJob and writing a perform method.

Use perform_later to enqueue jobs without blocking your app.

Practice

(1/5)
1. What is the main purpose of Active Job in Rails?
easy
A. To create database tables automatically
B. To run tasks in the background without blocking the main app
C. To handle user authentication
D. To style HTML pages with CSS

Solution

  1. Step 1: Understand Active Job's role

    Active Job is designed to run tasks asynchronously, meaning it runs jobs in the background.
  2. Step 2: Compare options with Active Job's purpose

    Database table creation, user authentication, and page styling are not Active Job's focus.
  3. Final Answer:

    To run tasks in the background without blocking the main app -> Option B
  4. Quick Check:

    Background tasks = A [OK]
Hint: Active Job runs tasks behind the scenes, not user-facing features [OK]
Common Mistakes:
  • Confusing Active Job with database migrations
  • Thinking Active Job handles user login
  • Assuming Active Job styles pages
2. Which of the following is the correct way to enqueue a job to run later in Active Job?
easy
A. MyJob.perform_later(args)
B. MyJob.perform_now(args)
C. MyJob.enqueue(args)
D. MyJob.run_async(args)

Solution

  1. Step 1: Recall Active Job enqueue methods

    Active Job uses perform_later to enqueue jobs to run asynchronously.
  2. Step 2: Check each option's validity

    perform_now runs immediately, enqueue and run_async are not valid Active Job methods.
  3. Final Answer:

    MyJob.perform_later(args) -> Option A
  4. Quick Check:

    Use perform_later to enqueue jobs [OK]
Hint: Use perform_later to run jobs later, not perform_now [OK]
Common Mistakes:
  • Using perform_now instead of perform_later
  • Trying to call enqueue or run_async which don't exist
  • Confusing method names
3. Given this job class:
class GreetingJob < ApplicationJob
  def perform(name)
    puts "Hello, #{name}!"
  end
end

GreetingJob.perform_later('Alice')

What will happen when this code runs?
medium
A. Prints 'Hello, Alice!' immediately
B. Raises a syntax error
C. Prints 'Hello, Alice!' in the background later
D. Does nothing because perform_later is invalid

Solution

  1. Step 1: Understand perform_later behavior

    The perform_later method enqueues the job to run asynchronously in the background.
  2. Step 2: Analyze the output timing

    The puts inside perform runs when the job executes later, not immediately.
  3. Final Answer:

    Prints 'Hello, Alice!' in the background later -> Option C
  4. Quick Check:

    perform_later runs job later, not now [OK]
Hint: perform_later delays execution; output appears later [OK]
Common Mistakes:
  • Thinking perform_later runs immediately
  • Assuming perform_later is invalid
  • Confusing perform_later with perform_now
4. Identify the error in this Active Job class:
class EmailJob < ApplicationJob
  def perform
    UserMailer.welcome_email.deliver_now
  end
end
medium
A. perform method should be private
B. deliver_now is not allowed inside jobs
C. Job class must not inherit ApplicationJob
D. Missing argument in perform method

Solution

  1. Step 1: Check perform method signature

    The mailer method likely needs a user argument, but perform has no parameters to receive it.
  2. Step 2: Understand job argument passing

    Jobs should accept arguments in perform to pass data like user info; missing argument causes errors.
  3. Final Answer:

    Missing argument in perform method -> Option D
  4. Quick Check:

    perform needs parameters for job data [OK]
Hint: perform method must accept needed arguments [OK]
Common Mistakes:
  • Ignoring missing parameters in perform
  • Thinking deliver_now is forbidden in jobs
  • Believing ApplicationJob inheritance is wrong
5. You want to send a welcome email to a user after they sign up, but only if their email is verified. Which is the best way to use Active Job for this?
hard
A. Call WelcomeEmailJob.perform_later(user) only after checking user.email_verified?
B. Call WelcomeEmailJob.perform_later(user) unconditionally and check verification inside the job
C. Send the email directly in the controller without using Active Job
D. Use perform_now to send the email immediately regardless of verification

Solution

  1. Step 1: Understand conditional job enqueueing

    It's best to enqueue the job only if the user is verified to avoid unnecessary background work.
  2. Step 2: Compare options for efficiency and correctness

    Sending synchronously in the controller blocks the web request; enqueuing unconditionally wastes resources by processing jobs that may do nothing; using perform_now ignores verification and blocks the request.
  3. Final Answer:

    Call WelcomeEmailJob.perform_later(user) only after checking user.email_verified? -> Option A
  4. Quick Check:

    Enqueue jobs conditionally for efficiency [OK]
Hint: Check conditions before enqueueing jobs to save resources [OK]
Common Mistakes:
  • Enqueueing jobs without checking conditions
  • Sending emails synchronously blocking app
  • Ignoring user verification status
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard