Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Presence 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 presence validation is saved with a blank attribute?
Consider a Rails model with validates :name, presence: true. What will be the result of model.save if name is blank?
Ruby on Rails
class User < ApplicationRecord
  validates :name, presence: true
end

user = User.new(name: "")
saved = user.save
AThe save returns false and the user is not saved to the database.
BThe save returns true and the user is saved with an empty name.
CThe save raises a runtime error because name is blank.
DThe save returns nil and the user is partially saved.
Attempts:
2 left
💡 Hint
Think about what presence validation checks before saving.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds presence validation for multiple attributes?
You want to validate presence of both email and username in a Rails model. Which code is correct?
Ruby on Rails
class Account < ApplicationRecord
  # Add presence validation here
end
Avalidates :email, :username, presence: true
Bvalidates [:email, :username], presence: true
Cvalidates :email, presence: true, :username, presence: true
Dvalidates presence: true, attributes: [:email, :username]
Attempts:
2 left
💡 Hint
Check the syntax for multiple attributes in validates.
🔧 Debug
advanced
2:00remaining
Why does this presence validation not work as expected?
Given this model code, why does saving a user with blank name still succeed?
class User < ApplicationRecord
  validates :name, presence: true, allow_blank: true
end

user = User.new(name: "")
saved = user.save
AThe name attribute is not a string, so validation is skipped.
BThe presence validation requires allow_blank: false to work, which is missing here.
CThe save method is overridden and ignores validations.
DThe option allow_blank: true disables the presence validation, so blank names are allowed.
Attempts:
2 left
💡 Hint
Check what allow_blank: true does in presence validation.
❓ state_output
advanced
2:00remaining
What is the content of errors after failed presence validation?
After trying to save a model with blank title and presence validation, what will model.errors[:title] contain?
Ruby on Rails
class Post < ApplicationRecord
  validates :title, presence: true
end

post = Post.new(title: "")
post.save
errors = post.errors[:title]
A["is invalid"]
B[] (empty array)
C["can't be blank"]
Dnil
Attempts:
2 left
💡 Hint
What message does presence validation add on failure?
🧠 Conceptual
expert
2:00remaining
How does presence validation treat whitespace-only strings?
In Rails, if a model has validates :description, presence: true, what happens when description is a string with only spaces, like " "?
AThe validation raises an error due to invalid input type.
BThe validation fails because whitespace-only strings are treated as blank.
CThe validation passes because the string is not empty.
DThe validation ignores whitespace and saves the record.
Attempts:
2 left
💡 Hint
Think about how Rails treats strings with only spaces in presence validation.

Practice

(1/5)
1. What does validates :name, presence: true do in a Rails model?
easy
A. It ensures the name field is not empty before saving.
B. It automatically fills the name field with a default value.
C. It deletes the record if name is empty.
D. It allows name to be empty without errors.

Solution

  1. Step 1: Understand presence validation purpose

    Presence validation checks that a field is not blank before saving a record.
  2. Step 2: Apply to name field

    Using validates :name, presence: true means the name must have a value.
  3. Final Answer:

    It ensures the name field is not empty before saving. -> Option A
  4. Quick Check:

    Presence validation = field not empty [OK]
Hint: Presence validation means field cannot be blank [OK]
Common Mistakes:
  • Thinking it fills default values automatically
  • Confusing presence with uniqueness validation
  • Assuming it deletes records if empty
2. Which of the following is the correct syntax to add presence validation for the email attribute in a Rails model?
easy
A. validates_presence_of email
B. validates :email, presence: true
C. validate :email, presence: true
D. validates :email, required: true

Solution

  1. Step 1: Recall modern Rails validation syntax

    Rails uses validates :attribute, presence: true for presence validation.
  2. Step 2: Check each option

    validates_presence_of email is invalid syntax (missing symbol ":email"); validates :email, presence: true is the correct modern syntax; validate :email, presence: true is incorrect method name; validates :email, required: true uses wrong option key.
  3. Final Answer:

    validates :email, presence: true -> Option B
  4. Quick Check:

    Use validates :field, presence: true [OK]
Hint: Use validates :field, presence: true for presence [OK]
Common Mistakes:
  • Using validate instead of validates
  • Using required instead of presence
  • Confusing old and new syntax
3. Given this model code:
class User < ApplicationRecord
  validates :username, presence: true
end

What happens if you try to save User.new(username: '')?
medium
A. The save fails because username is blank.
B. The user is saved with an empty username.
C. An error is raised due to syntax error.
D. The username is automatically set to 'default'.

Solution

  1. Step 1: Understand presence validation effect on blank string

    Presence validation treats empty strings as invalid, so saving fails.
  2. Step 2: Apply to username: ''

    Since username is empty string, validation fails and save returns false.
  3. Final Answer:

    The save fails because username is blank. -> Option A
  4. Quick Check:

    Empty string fails presence validation [OK]
Hint: Empty string fails presence validation, save returns false [OK]
Common Mistakes:
  • Thinking empty string passes presence validation
  • Expecting automatic default value assignment
  • Confusing validation failure with exceptions
4. Identify the error in this model validation code:
class Product < ApplicationRecord
  validates :title, presence
end
medium
A. No error, code is correct.
B. Should use validate instead of validates.
C. The attribute title must be a string.
D. Missing true after presence.

Solution

  1. Step 1: Check presence validation syntax

    Presence validation requires presence: true, not just presence.
  2. Step 2: Identify missing true

    The code misses : true after presence, causing syntax error.
  3. Final Answer:

    Missing true after presence. -> Option D
  4. Quick Check:

    Presence validation needs presence: true [OK]
Hint: Always write presence: true for presence validation [OK]
Common Mistakes:
  • Omitting : true after presence
  • Using validate instead of validates
  • Assuming attribute type affects validation syntax
5. You want to ensure a Post model's title and content are present, but content can be blank if title is 'Draft'. Which validation setup achieves this?
hard
A. validates :title, presence: true validates :content, presence: false, unless: -> { title == 'Draft' }
B. validates :title, presence: true validates :content, presence: true, if: -> { title == 'Draft' }
C. validates :title, presence: true validates :content, presence: true, unless: -> { title == 'Draft' }
D. validates :title, presence: true validates :content, presence: false, if: -> { title == 'Draft' }

Solution

  1. Step 1: Understand requirement for content

    Content must be present except when title is 'Draft'.
  2. Step 2: Use conditional presence validation

    Use presence: true with unless condition to skip validation if title is 'Draft'.
  3. Step 3: Check options

    validates :title, presence: true validates :content, presence: true, unless: -> { title == 'Draft' } correctly uses unless: -> { title == 'Draft' } to skip content presence validation when title is 'Draft'. Others misuse if or set presence false incorrectly.
  4. Final Answer:

    validates :title, presence: true validates :content, presence: true, unless: -> { title == 'Draft' } -> Option C
  5. Quick Check:

    Use unless to skip presence validation conditionally [OK]
Hint: Use unless to skip presence validation conditionally [OK]
Common Mistakes:
  • Using if instead of unless for skipping validation
  • Setting presence: false which disables validation
  • Not combining validations properly
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