0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.