Bird
Raised Fist0
Ruby on Railsframework~10 mins

Active Job framework in Ruby on Rails - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enqueue a job to run asynchronously.

Ruby on Rails
MyJob.perform_later([1])
Drag options to blanks, or click blank then click option'
Auser_id
Bperform_now
Cenqueue
Dperform_later
Attempts:
3 left
💡 Hint
Common Mistakes
Using perform_now instead of perform_later
Forgetting to pass the job argument
Calling enqueue which is not a method here
2fill in blank
medium

Complete the code to define a job class inheriting from ActiveJob.

Ruby on Rails
class MyJob < [1]
  def perform(user_id)
    # job code here
  end
end
Drag options to blanks, or click blank then click option'
AApplicationJob
BActiveJob
CJobBase
DActiveJob::Base
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from ApplicationJob without defining it
Using just ActiveJob instead of ActiveJob::Base
Using a non-existent class JobBase
3fill in blank
hard

Fix the error in the job perform method to accept multiple arguments.

Ruby on Rails
def perform([1])
  # code using user_id and message
end
Drag options to blanks, or click blank then click option'
Auser_id, message
B*args
Cargs
Duser_id message
Attempts:
3 left
💡 Hint
Common Mistakes
Listing multiple arguments without commas
Using a single argument without splat for multiple values
Using an undefined variable name
4fill in blank
hard

Fill both blanks to schedule a job to run 5 minutes from now.

Ruby on Rails
MyJob.set(wait: [1]).perform_later([2])
Drag options to blanks, or click blank then click option'
A5.minutes
B10.seconds
Cuser.id
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using seconds instead of minutes for delay
Passing the whole user object instead of user ID
Omitting the set method
5fill in blank
hard

Fill all three blanks to retry a job on failure with a maximum of 3 attempts and 5 seconds wait between retries.

Ruby on Rails
class RetryJob < ActiveJob::Base
  retry_on [1], wait: [2], attempts: [3]

  def perform
    # job code
  end
end
Drag options to blanks, or click blank then click option'
AStandardError
B5.seconds
C3
DRuntimeError
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong error class
Mixing up wait time units
Setting attempts to zero or negative

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