Challenge - 5 Problems
Conditional Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when conditional validation is triggered?
Consider this Rails model snippet:
What is the validation behavior when a User is 17 years old and email is blank?
class User < ApplicationRecord
validates :email, presence: true, if: :email_required?
def email_required?
age >= 18
end
endWhat is the validation behavior when a User is 17 years old and email is blank?
Attempts:
2 left
💡 Hint
Look at the condition method email_required? and when it returns true.
✗ Incorrect
The validation for email presence runs only if email_required? returns true. Since age is 17, email_required? returns false, so email presence is not validated.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax for conditional validation using :unless
Which option correctly uses :unless to skip validation when the user is an admin?
class User < ApplicationRecord # Validate presence of password unless user is admin end
Attempts:
2 left
💡 Hint
The :unless option expects a symbol referencing a method name.
✗ Incorrect
The :unless option takes a symbol for a method name that returns true or false. Option B correctly uses :admin? as a symbol.
🔧 Debug
advanced2:00remaining
Why does this conditional validation never run?
Given this model:
Why might the price validation never trigger even when sale is true?
class Product < ApplicationRecord
validates :price, numericality: true, if: :on_sale
def on_sale
sale == true
end
endWhy might the price validation never trigger even when sale is true?
Attempts:
2 left
💡 Hint
Check the data type of the sale attribute and how Ruby compares true with 'true'.
✗ Incorrect
If sale is stored as a string 'true', the comparison sale == true returns false, so on_sale returns false and validation does not run.
❓ state_output
advanced2:00remaining
What is the validation error output for this conditional validation?
Consider this model:
What is the output of
class Order < ApplicationRecord
validates :shipping_address, presence: true, if: -> { delivery_required }
def delivery_required
delivery == 'yes'
end
end
order = Order.new(delivery: 'yes', shipping_address: '')
order.valid?
order.errors.full_messagesWhat is the output of
order.errors.full_messages?Attempts:
2 left
💡 Hint
The validation runs only if delivery_required returns true.
✗ Incorrect
Since delivery is 'yes', delivery_required returns true, so shipping_address presence is validated. It is blank, so error is added.
🧠 Conceptual
expert3:00remaining
Which conditional validation approach is best for complex logic?
You want to validate an attribute only if multiple conditions are met, including checking user role, time of day, and a feature flag. Which approach is best in Rails?
Attempts:
2 left
💡 Hint
Think about maintainability and clarity for complex conditions.
✗ Incorrect
A single method encapsulating all logic keeps validation clean and easy to maintain. Lambdas can be used but may become hard to read if complex.