0
0
Ruby on Railsframework~20 mins

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

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