0
0
Ruby on Railsframework~20 mins

Conditional validations in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when conditional validation is triggered?
Consider this Rails model snippet:
class User < ApplicationRecord
  validates :email, presence: true, if: :email_required?

  def email_required?
    age >= 18
  end
end

What is the validation behavior when a User is 17 years old and email is blank?
AValidation passes because email presence is only checked if age is 18 or more.
BValidation passes only if email is blank and age is exactly 17.
CValidation fails because age is less than 18 but email is blank.
DValidation fails because email must always be present.
Attempts:
2 left
💡 Hint
Look at the condition method email_required? and when it returns true.
📝 Syntax
intermediate
2: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
Avalidates :password, presence: true, unless: admin?
Bvalidates :password, presence: true, unless: :admin?
Cvalidates :password, presence: true, unless: admin
Dvalidates :password, presence: true, unless: -> { admin? }
Attempts:
2 left
💡 Hint
The :unless option expects a symbol referencing a method name.
🔧 Debug
advanced
2:00remaining
Why does this conditional validation never run?
Given this model:
class Product < ApplicationRecord
  validates :price, numericality: true, if: :on_sale

  def on_sale
    sale == true
  end
end

Why might the price validation never trigger even when sale is true?
AThe method on_sale should be named on_sale? with a question mark.
BThe if option expects a symbol, but :on_sale is a method returning a boolean, so it works fine.
CThe validation needs to use unless instead of if to work correctly.
DThe sale attribute might be a string 'true' instead of boolean true, causing on_sale to return false.
Attempts:
2 left
💡 Hint
Check the data type of the sale attribute and how Ruby compares true with 'true'.
state_output
advanced
2:00remaining
What is the validation error output for this conditional validation?
Consider this model:
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_messages

What is the output of order.errors.full_messages?
A[] (empty array)
B["Delivery must be yes"]
C["Shipping address can't be blank"]
D["Shipping address is invalid"]
Attempts:
2 left
💡 Hint
The validation runs only if delivery_required returns true.
🧠 Conceptual
expert
3: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?
AUse a single method that returns true or false combining all conditions, then use if: :method_name.
BChain multiple validations each with simple if conditions for each check separately.
CUse a lambda with all conditions inside for the if option.
DUse validates with unless option negating all conditions.
Attempts:
2 left
💡 Hint
Think about maintainability and clarity for complex conditions.