Bird
Raised Fist0
Ruby on Railsframework~20 mins

Numericality validation in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numericality Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a model with numericality validation receives a non-numeric value?
Consider a Rails model with validates :age, numericality: true. What will be the validation result if age is set to 'twenty'?
Ruby on Rails
class User < ApplicationRecord
  validates :age, numericality: true
end

user = User.new(age: 'twenty')
user.valid?
user.errors.full_messages
AValidation raises a runtime error because 'twenty' cannot be converted to a number.
BValidation passes because 'twenty' is a string and strings are allowed.
CValidation fails with an error message saying age is not a number.
DValidation ignores the age attribute and passes.
Attempts:
2 left
💡 Hint
Think about what numericality validation expects as input.
📝 Syntax
intermediate
2:00remaining
Which validation syntax correctly enforces an integer value only?
You want to validate that the attribute quantity is an integer number only. Which of the following validation lines is correct?
Avalidates :quantity, numericality: { only_integer: true }
Bvalidates :quantity, numericality: true, only_integer: true
Cvalidates :quantity, only_integer: true
Dvalidates :quantity, numericality: { integer_only: true }
Attempts:
2 left
💡 Hint
Check the correct option key inside the numericality hash.
❓ state_output
advanced
2:00remaining
What is the error message when a number is less than the minimum allowed?
Given the validation validates :score, numericality: { greater_than_or_equal_to: 10 }, what error message appears if score is set to 5?
Ruby on Rails
class Game < ApplicationRecord
  validates :score, numericality: { greater_than_or_equal_to: 10 }
end

game = Game.new(score: 5)
game.valid?
game.errors.full_messages
A["Score must be greater than 10"]
B["Score must be at least 5"]
C["Score is not a number"]
D["Score must be greater than or equal to 10"]
Attempts:
2 left
💡 Hint
Look carefully at the validation option used.
🔧 Debug
advanced
2:00remaining
Why does this numericality validation not work as expected?
This model validation is intended to allow only positive integers, but it accepts negative numbers. Why? validates :count, numericality: { only_integer: true, greater_than: 0 }
Ruby on Rails
class Item < ApplicationRecord
  validates :count, numericality: { only_integer: true, greater_than: 0 }
end

item = Item.new(count: -3)
item.valid?
item.errors.full_messages
AThe validation fails because the attribute is a string, not an integer.
BThe validation works correctly; negative numbers are rejected with an error.
CThe validation is missing the presence check, so nil values pass.
DThe validation allows negative numbers because only_integer does not check sign.
Attempts:
2 left
💡 Hint
Try running the code and check the errors.
🧠 Conceptual
expert
2:00remaining
How does the allow_nil option affect numericality validation?
Consider validates :price, numericality: true, allow_nil: true. What is the behavior when price is nil?
AValidation passes and no error is added because nil is allowed.
BValidation fails because nil is not a number.
CValidation ignores the attribute completely, even if it has a value.
DValidation raises an exception due to nil value.
Attempts:
2 left
💡 Hint
Think about what allow_nil means for validations.

Practice

(1/5)
1. What does the validates :age, numericality: true line do in a Rails model?
easy
A. It checks if age is a string.
B. It makes the age attribute required.
C. It ensures the age attribute is a number.
D. It sets a default value for age.

Solution

  1. Step 1: Understand numericality validation

    The numericality: true option checks if the attribute is a number.
  2. Step 2: Apply to age attribute

    Since it is applied to age, it ensures age must be numeric.
  3. Final Answer:

    It ensures the age attribute is a number. -> Option C
  4. Quick Check:

    Numericality validation = ensures number [OK]
Hint: Numericality means the value must be a number [OK]
Common Mistakes:
  • Confusing numericality with presence validation
  • Thinking it sets default values
  • Assuming it checks for strings
2. Which of the following is the correct syntax to validate that price is an integer in Rails?
easy
A. validates :price, numericality: { only_integer: true }
B. validates :price, integer: true
C. validates :price, number: { integer: true }
D. validates :price, numericality: true, integer: true

Solution

  1. Step 1: Recall correct numericality syntax

    Rails uses numericality: { only_integer: true } to check integers.
  2. Step 2: Check options

    validates :price, numericality: { only_integer: true } matches this syntax exactly; others are invalid or incorrect.
  3. Final Answer:

    validates :price, numericality: { only_integer: true } -> Option A
  4. Quick Check:

    Integer validation uses only_integer: true [OK]
Hint: Use only_integer inside numericality hash [OK]
Common Mistakes:
  • Using a separate integer key instead of inside numericality
  • Writing invalid keys like number or integer alone
  • Missing curly braces for options
3. Given this model validation:
validates :score, numericality: { greater_than: 0, less_than_or_equal_to: 100 }

What happens if you try to save score = 150?
medium
A. Validation fails because 150 is greater than 100.
B. The record saves successfully.
C. Validation fails because 150 is less than 0.
D. Validation passes because 150 is a number.

Solution

  1. Step 1: Understand validation limits

    The validation requires score to be greater than 0 and less than or equal to 100.
  2. Step 2: Check the value 150

    150 is greater than 100, so it violates the less_than_or_equal_to: 100 rule.
  3. Final Answer:

    Validation fails because 150 is greater than 100. -> Option A
  4. Quick Check:

    Value must be ≤ 100, 150 fails [OK]
Hint: Check all numerical limits carefully [OK]
Common Mistakes:
  • Ignoring upper limit and assuming number is valid
  • Confusing greater_than with greater_than_or_equal_to
  • Assuming any number passes
4. Identify the error in this validation:
validates :quantity, numericality: { only_integer: true, greater_than: 0, less_than: 10, message: 'must be a number' }
medium
A. There is no error; this validation is correct.
B. The message option is misplaced inside the numericality hash.
C. The validation is missing presence: true.
D. The only_integer option cannot be combined with greater_than.

Solution

  1. Step 1: Check placement of message

    The message option should be outside the numericality options hash, not inside it.
  2. Step 2: Validate other options

    only_integer, greater_than, and less_than can be combined correctly.
  3. Final Answer:

    The message option is misplaced inside the numericality hash. -> Option B
  4. Quick Check:

    Message option goes outside numericality hash [OK]
Hint: Put message outside numericality options hash [OK]
Common Mistakes:
  • Placing message inside numericality hash
  • Thinking only_integer conflicts with other options
  • Assuming presence is required for numericality
5. You want to validate a rating attribute that must be an integer between 1 and 5 inclusive. Which validation code correctly enforces this?
hard
A. validates :rating, numericality: { only_integer: true, greater_than: 1, less_than: 5 }
B. validates :rating, numericality: { only_integer: true, greater_than: 1, less_than_or_equal_to: 5 }
C. validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than: 5 }
D. validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 }

Solution

  1. Step 1: Understand inclusive range requirements

    The rating must include 1 and 5, so use greater_than_or_equal_to: 1 and less_than_or_equal_to: 5.
  2. Step 2: Check integer enforcement

    only_integer: true ensures rating is an integer.
  3. Step 3: Compare options

    validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 } matches all requirements exactly; others exclude 1 or 5 or use wrong operators.
  4. Final Answer:

    validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 } -> Option D
  5. Quick Check:

    Inclusive integer range uses >= and <= [OK]
Hint: Use greater_than_or_equal_to and less_than_or_equal_to for inclusive range [OK]
Common Mistakes:
  • Using greater_than or less_than excludes boundary values
  • Forgetting only_integer for integer check
  • Mixing up operators for inclusive range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard