0
0
Ruby on Railsframework~20 mins

Form object pattern in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the form object validates with invalid data?

Given a Rails form object that validates presence of a user's email and password, what will be the state of the form object after calling valid? with missing email?

Ruby on Rails
class UserForm
  include ActiveModel::Model
  attr_accessor :email, :password
  validates :email, presence: true
  validates :password, presence: true
end

form = UserForm.new(email: nil, password: 'secret')
form.valid?
AThe form object raises an exception due to nil email.
BThe form object is valid because password is present.
CThe form object is invalid and errors include missing email message.
DThe form object is valid but errors include missing email.
Attempts:
2 left
💡 Hint

Think about how ActiveModel::Model validations work when required attributes are missing.

state_output
intermediate
2:00remaining
What is the value of user.name after form submission?

Consider a form object that assigns attributes to a User model on save. After calling form.save with name 'Alice', what is the value of user.name?

Ruby on Rails
class UserForm
  include ActiveModel::Model
  attr_accessor :name, :user

  def save
    return false unless valid?
    user.name = name
    true
  end
end

user = User.new(name: 'Bob')
form = UserForm.new(name: 'Alice', user: user)
form.save
user.name
ARaises NoMethodError
B'Alice'
Cnil
D'Bob'
Attempts:
2 left
💡 Hint

Remember the form object assigns the name attribute to the user on successful save.

📝 Syntax
advanced
2:30remaining
Which option correctly defines a form object with nested attributes?

Which code snippet correctly implements a Rails form object that accepts nested attributes for an associated Profile model?

A
class UserForm
  include ActiveModel::Model
  attr_accessor :name, :profile_attributes
  validates :name, presence: true
  def save
    user = User.new(name: name)
    user.build_profile(profile_attributes)
    user.save
  end
end
B
class UserForm
  include ActiveModel::Model
  attr_accessor :name, :profile
  validates :name, presence: true
  accepts_nested_attributes_for :profile
  def save
    user = User.new(name: name)
    user.profile = profile
    user.save
  end
end
C
class UserForm
  include ActiveModel::Model
  attr_accessor :name, :profile_attributes
  validates :name, presence: true
  def save
    user = User.new(name: name)
    user.profile_attributes = profile_attributes
    user.save
  end
end
D
class UserForm
  include ActiveModel::Model
  attr_accessor :name, :profile_attributes
  validates :name, presence: true
  def save
    user = User.new(name: name)
    user.profile = Profile.new(profile_attributes)
    user.save
  end
end
Attempts:
2 left
💡 Hint

Remember that accepts_nested_attributes_for is a model method, not available in plain form objects.

🔧 Debug
advanced
2:30remaining
Why does this form object fail to save the user?

Given the following form object, why does this form object fail to persist the user even when valid data is provided?

Ruby on Rails
class UserForm
  include ActiveModel::Model
  attr_accessor :email, :user
  validates :email, presence: true

  def save
    return false unless valid?
    user.email = email
  end
end

user = User.new
form = UserForm.new(email: 'test@example.com', user: user)
form.save
AThe save method does not call user.save, so changes are not persisted.
BThe form object is missing a password attribute, causing validation failure.
CThe user object is nil, so assignment fails with NoMethodError.
DThe email attribute is not permitted, causing strong parameters error.
Attempts:
2 left
💡 Hint

Check if the user model is saved to the database after assignment.

🧠 Conceptual
expert
3:00remaining
What is the main benefit of using a form object pattern in Rails?

Why would a developer choose to use a form object instead of directly using ActiveRecord models in a Rails application?

ATo separate complex form validation and business logic from the database models, improving code organization and testability.
BTo automatically generate database migrations and schema from form inputs.
CTo replace ActiveRecord models entirely and avoid using the database.
DTo speed up page rendering by caching form data in memory.
Attempts:
2 left
💡 Hint

Think about how form objects help manage complexity in large applications.