0
0
Ruby on Railsframework~20 mins

Callbacks overview in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Callbacks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a before_save callback returns false?

In Rails, if a before_save callback returns false, what is the effect on the save operation?

Ruby on Rails
class User < ApplicationRecord
  before_save :check_status

  def check_status
    return false if inactive?
  end
end

user = User.new(status: 'inactive')
user.save
AThe save operation continues normally and the record is saved.
BThe save operation is aborted and the record is not saved.
CAn exception is raised stopping the save operation.
DThe callback is ignored and save proceeds.
Attempts:
2 left
💡 Hint

Think about how returning false in a before_save callback affects the transaction.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly defines an after_commit callback?

Choose the correct way to define an after_commit callback in a Rails model.

Aafter_commit :send_notification
Bafter_commit do send_notification end
Cafter_commit -> { send_notification }
Dafter_commit :send_notification, on: :create
Attempts:
2 left
💡 Hint

Consider the options for specifying when the callback runs.

🔧 Debug
advanced
2:00remaining
Why does this before_destroy callback not prevent deletion?

Given the following model, why does the record still get deleted despite the before_destroy callback?

Ruby on Rails
class Post < ApplicationRecord
  before_destroy :check_if_published

  def check_if_published
    throw(:abort) if published?
  end
end

post = Post.find(1)
post.destroy
AThe <code>published?</code> method always returns false, so deletion proceeds.
BThe callback uses <code>throw(:abort)</code> which is correct, so the record should not be deleted.
CThe callback should return false instead of throwing :abort to stop deletion.
DThe callback is not registered properly because it lacks <code>self.</code> prefix.
Attempts:
2 left
💡 Hint

Check the condition inside the callback method.

state_output
advanced
2:00remaining
What is the value of status after save with after_save callback?

Consider this model with an after_save callback that changes an attribute. What is the value of status after save completes?

Ruby on Rails
class Task < ApplicationRecord
  after_save :mark_completed

  def mark_completed
    update_column(:status, 'completed')
  end
end

task = Task.create(status: 'pending')
task.reload
status_value = task.status
A"completed"
BRaises an error due to recursive update
Cnil
D"pending"
Attempts:
2 left
💡 Hint

Think about when after_save runs and what update_column does.

🧠 Conceptual
expert
2:00remaining
Which callback ensures code runs only after a successful database commit?

In Rails, which callback guarantees that the code inside it runs only after the database transaction has been successfully committed?

Aafter_create
Bafter_save
Cafter_commit
Dafter_validation
Attempts:
2 left
💡 Hint

Think about the difference between saving and committing a transaction.