📖 Scenario: You are building a simple Rails application to manage products. Each product has a price that must be a number. To keep data clean, you want to ensure that the price is always a number when saving a product.
🎯 Goal: Learn how to add numericality validation to a Rails model attribute to ensure only numbers are accepted.
📋 What You'll Learn
Create a Rails model called Product with an attribute price
Add a numericality validation to the price attribute
Allow only numeric values for price
Ensure the validation is active when saving a product
💡 Why This Matters
🌍 Real World
Validations like numericality ensure that data entered into your app is correct and prevents errors later on.
💼 Career
Rails developers often add validations to models to enforce business rules and data integrity.
Progress0 / 4 steps
1
Create the Product model with a price attribute
Create a Rails model called Product with an attribute price of type decimal.
Ruby on Rails
Hint
Use rails generate model Product price:decimal to create the model and migration.
2
Add a numericality validation configuration
Inside the Product model, add a validation line that sets up numericality validation for the price attribute.
Ruby on Rails
Hint
Use validates :price, numericality: true inside the model class.
3
Test the numericality validation in the model
Write a method inside the Product model called valid_price? that returns true if the product is valid and false otherwise, using the built-in valid? method.
Ruby on Rails
Hint
Use def valid_price? and inside it call valid? to check validations.
4
Complete the model with validation and method
Ensure the Product model includes the price attribute, the numericality validation, and the valid_price? method as defined.
Ruby on Rails
Hint
Make sure all parts are present: class, validation, and method.
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
Step 1: Understand numericality validation
The numericality: true option checks if the attribute is a number.
Step 2: Apply to age attribute
Since it is applied to age, it ensures age must be numeric.
Final Answer:
It ensures the age attribute is a number. -> Option C
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
Step 1: Recall correct numericality syntax
Rails uses numericality: { only_integer: true } to check integers.
Step 2: Check options
validates :price, numericality: { only_integer: true } matches this syntax exactly; others are invalid or incorrect.
Final Answer:
validates :price, numericality: { only_integer: true } -> Option A
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