Bird
Raised Fist0
Ruby on Railsframework~30 mins

Active Job framework in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style10 modes available
Background Email Sending with Active Job
📖 Scenario: You are building a Rails app that sends welcome emails to new users. To avoid slowing down the app, you want to send emails in the background using Active Job.
🎯 Goal: Build a simple Active Job class that sends a welcome email asynchronously when a new user is created.
📋 What You'll Learn
Create a job class called WelcomeEmailJob
Add a perform method that calls UserMailer.welcome_email(user)
Enqueue the job with a user instance
Configure the job to use the async adapter
💡 Why This Matters
🌍 Real World
Background jobs like sending emails improve user experience by not blocking web requests.
💼 Career
Understanding Active Job is essential for Rails developers to build scalable and responsive applications.
Progress0 / 4 steps
1
Create the WelcomeEmailJob class
Create a job class called WelcomeEmailJob that inherits from ApplicationJob.
Ruby on Rails
Hint

Use class WelcomeEmailJob < ApplicationJob to define the job class.

2
Add the perform method
Inside WelcomeEmailJob, add a perform method that takes a user parameter and calls UserMailer.welcome_email(user).deliver_now.
Ruby on Rails
Hint

The perform method is where the job does its work.

3
Enqueue the job with a user instance
Write code to enqueue WelcomeEmailJob with a variable called user using perform_later.
Ruby on Rails
Hint

Use perform_later to enqueue the job asynchronously.

4
Configure Active Job to use async adapter
Set the Active Job queue adapter to :async by adding config.active_job.queue_adapter = :async inside the Rails.application.configure block.
Ruby on Rails
Hint

This configuration makes Rails run jobs in the background using threads.

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