0
0
Ruby on Railsframework~10 mins

Why validations protect data integrity in Ruby on Rails - Test Your Understanding

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

Complete the code to add a validation that ensures the presence of a name.

Ruby on Rails
class User < ApplicationRecord
  validates :name, [1]
end
Drag options to blanks, or click blank then click option'
Alength: { minimum: 3 }
Bnumericality: true
Cuniqueness: true
Dpresence: true
Attempts:
3 left
💡 Hint
Common Mistakes
Using length validation instead of presence
Using uniqueness when presence is needed
2fill in blank
medium

Complete the code to validate that the email is unique.

Ruby on Rails
class User < ApplicationRecord
  validates :email, [1]
end
Drag options to blanks, or click blank then click option'
Auniqueness: true
Blength: { maximum: 50 }
Cformat: { with: /@/ }
Dpresence: true
Attempts:
3 left
💡 Hint
Common Mistakes
Using presence instead of uniqueness
Using format validation only
3fill in blank
hard

Fix the error in the validation to check that age is a number greater than or equal to 18.

Ruby on Rails
class User < ApplicationRecord
  validates :age, numericality: { [1]: 18 }
end
Drag options to blanks, or click blank then click option'
Agreater_than
Bgreater_than_or_equal_to
Cless_than
Dequal_to
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater_than which excludes 18
Using less_than or equal_to incorrectly
4fill in blank
hard

Fill both blanks to validate that the username is present and has a minimum length of 5.

Ruby on Rails
class User < ApplicationRecord
  validates :username, [1], [2]
end
Drag options to blanks, or click blank then click option'
Apresence: true
Blength: { minimum: 5 }
Cuniqueness: true
Dnumericality: true
Attempts:
3 left
💡 Hint
Common Mistakes
Using uniqueness instead of presence
Forgetting to check length
5fill in blank
hard

Fill all three blanks to validate that the password is present, has a minimum length of 8, and includes a confirmation field.

Ruby on Rails
class User < ApplicationRecord
  validates :password, [1], [2], [3]
end
Drag options to blanks, or click blank then click option'
Apresence: true
Blength: { minimum: 8 }
Cconfirmation: true
Duniqueness: true
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting confirmation validation
Using uniqueness instead of confirmation