0
0
Ruby on Railsframework~5 mins

Presence validation in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is presence validation in Rails?
Presence validation ensures that a specific attribute is not empty or nil before saving a record in the database.
Click to reveal answer
beginner
How do you add presence validation to a model attribute in Rails?
Use validates :attribute_name, presence: true inside the model class.
Click to reveal answer
beginner
What happens if a presence validation fails when saving a record?
The record is not saved, and error messages are added to the model's errors collection.
Click to reveal answer
intermediate
Can presence validation check multiple attributes at once?
Yes, you can validate multiple attributes by listing them: validates :name, :email, presence: true.
Click to reveal answer
beginner
Why is presence validation important in web applications?
It prevents saving incomplete or invalid data, ensuring the app works correctly and data stays reliable.
Click to reveal answer
Which Rails method is used to enforce presence validation on a model attribute?
Avalidates :attribute, presence: true
Bvalidate_presence_of :attribute
Ccheck_presence :attribute
Drequire :attribute
What will happen if a model fails presence validation when calling save?
AThe record will save anyway
BThe attribute will be auto-filled
CThe app will crash
DThe record will not save and errors will be added
Can you validate presence for multiple attributes in one line?
AYes, but only for string attributes
BNo, you must validate each attribute separately
CYes, by listing attributes like <code>validates :name, :email, presence: true</code>
DNo, Rails does not support multiple attributes in presence validation
Which of these values will fail presence validation?
A"Hello"
B" " (a space)
Cnil
D0
Where do you place presence validation code in a Rails app?
AIn the model
BIn the database schema
CIn the view
DIn the controller
Explain how to add presence validation to a Rails model and what it does.
Think about where validations go and what presence means.
You got /3 concepts.
    Why is presence validation useful in a web application? Give an example.
    Consider what happens if important info is missing.
    You got /3 concepts.