0
0
Ruby on Railsframework~10 mins

Conditional validations in Ruby on Rails - Interactive Code Practice

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

Complete the code to validate presence of :email only if :newsletter is true.

Ruby on Rails
validates :email, presence: true, if: [1]
Drag options to blanks, or click blank then click option'
A:newsletter == true
Bnewsletter == true
C-> { newsletter }
D:newsletter
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a lambda for the :if option.
Writing a comparison directly without a lambda.
2fill in blank
medium

Complete the code to validate :age numericality only if :adult is true.

Ruby on Rails
validates :age, numericality: true, if: [1]
Drag options to blanks, or click blank then click option'
A:adult
B-> { adult }
Cadult == true
D-> adult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a symbol instead of a lambda.
Using invalid lambda syntax.
3fill in blank
hard

Fix the error in the validation to run only if :active is true.

Ruby on Rails
validates :username, presence: true, if: [1]
Drag options to blanks, or click blank then click option'
Aactive?
Bactive == true
C:active
D-> { active? }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a symbol without method name.
Using a boolean expression directly without a lambda.
4fill in blank
hard

Fill in the blank to validate :phone presence only if :contact_method equals 'phone'.

Ruby on Rails
validates :phone, presence: true, if: [1]
Drag options to blanks, or click blank then click option'
A-> { contact_method == 'phone' }
B:contact_method
C-> { contact_method === 'phone' }
Dcontact_method == 'phone'
Attempts:
3 left
💡 Hint
Common Mistakes
Using triple equals (===) which is not typical for string comparison.
Passing a symbol instead of a lambda.
5fill in blank
hard

Fill in the blank to validate :password presence only if :user_signed_in is false and :guest_user is true.

Ruby on Rails
validates :password, presence: true, if: [1]
Drag options to blanks, or click blank then click option'
A-> { !user_signed_in && guest_user }
B-> { user_signed_in == false && guest_user == true }
C-> { !user_signed_in and guest_user }
D-> { user_signed_in != true && guest_user }
Attempts:
3 left
💡 Hint
Common Mistakes
Using and instead of && which has different precedence.
Writing conditions outside a lambda.