0
0
Ruby on Railsframework~10 mins

Numericality validation 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 that the age attribute is a number.

Ruby on Rails
validates :age, [1]: true
Drag options to blanks, or click blank then click option'
Auniqueness
Bpresence
Cnumericality
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using presence instead of numericality
Using uniqueness which checks for duplicates
Using length which checks string length
2fill in blank
medium

Complete the code to validate that the price attribute is a number greater than 0.

Ruby on Rails
validates :price, numericality: { [1]: 0 }
Drag options to blanks, or click blank then click option'
Agreater_than
Bless_than
Cequal_to
Donly_integer
Attempts:
3 left
💡 Hint
Common Mistakes
Using less_than instead of greater_than
Using equal_to which checks for exact value
Using only_integer which restricts to integers only
3fill in blank
hard

Fix the error in the validation to allow only integer values for quantity.

Ruby on Rails
validates :quantity, numericality: { [1]: true }
Drag options to blanks, or click blank then click option'
Aonly_integer
Bonly_float
Cgreater_than_or_equal_to
Dless_than_or_equal_to
Attempts:
3 left
💡 Hint
Common Mistakes
Using only_float which allows decimals
Using greater_than_or_equal_to which sets a range
Using less_than_or_equal_to which sets a range
4fill in blank
hard

Fill both blanks to validate that score is an integer between 1 and 10 inclusive.

Ruby on Rails
validates :score, numericality: { only_integer: true, [1]: 1, [2]: 10 }
Drag options to blanks, or click blank then click option'
Agreater_than_or_equal_to
Bless_than
Cless_than_or_equal_to
Dgreater_than
Attempts:
3 left
💡 Hint
Common Mistakes
Using less_than instead of less_than_or_equal_to
Using greater_than instead of greater_than_or_equal_to
Mixing up the order of options
5fill in blank
hard

Fill all three blanks to validate that discount is a number, greater than or equal to 0, and less than 1.

Ruby on Rails
validates :discount, numericality: { [1]: true, [2]: 0, [3]: 1 }
Drag options to blanks, or click blank then click option'
Aonly_integer
Bgreater_than_or_equal_to
Cless_than
Dgreater_than
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater_than instead of greater_than_or_equal_to
Using less_than_or_equal_to instead of less_than
Omitting only_integer when integers are required