Complete the code to validate presence of :email only if :newsletter is true.
validates :email, presence: true, if: [1]
The if option accepts a lambda to conditionally run the validation. Here, -> { newsletter } checks if newsletter is true.
Complete the code to validate :age numericality only if :adult is true.
validates :age, numericality: true, if: [1]
The if option needs a lambda that returns true when adult is true. -> { adult } works correctly.
Fix the error in the validation to run only if :active is true.
validates :username, presence: true, if: [1]
The if option expects a lambda or symbol. Using -> { active? } correctly calls the method to check if the record is active.
Fill in the blank to validate :phone presence only if :contact_method equals 'phone'.
validates :phone, presence: true, if: [1]
The if option needs a lambda that returns true when contact_method equals 'phone'. The lambda -> { contact_method == 'phone' } works correctly.
Fill in the blank to validate :password presence only if :user_signed_in is false and :guest_user is true.
validates :password, presence: true, if: [1]
and instead of && which has different precedence.The if option needs a lambda that returns true only when user_signed_in is false and guest_user is true. The lambda -> { !user_signed_in && guest_user } is clear and correct.