0
0
Ruby on Railsframework~20 mins

Nested attributes in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Attributes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when saving nested attributes with reject_if condition?

Consider a Rails model Order that accepts_nested_attributes_for :items with reject_if: :all_blank. What will be the result when saving an order with one item having all blank fields?

Ruby on Rails
class Order < ApplicationRecord
  has_many :items
  accepts_nested_attributes_for :items, reject_if: :all_blank
end

order = Order.new(items_attributes: [{name: '', quantity: ''}])
order.save
items_count = order.items.count
AThe order saves and items_count is nil because items are not loaded
BThe order saves and one blank item is created, so items_count is 1
CThe order fails to save due to validation errors on items
DThe order saves but no items are created, so items_count is 0
Attempts:
2 left
💡 Hint

Think about what reject_if: :all_blank does when nested attributes are blank.

📝 Syntax
intermediate
2:00remaining
Which nested attributes syntax correctly updates a child record?

Given a Post model with many comments and accepts_nested_attributes_for :comments, which params hash correctly updates the comment with id 5?

Ruby on Rails
params = {
  post: {
    comments_attributes: {
      '5' => { content: 'Updated comment' }
    }
  }
}
A{ post: { comments_attributes: { '5' => { content: 'Updated comment' } } } }
B{ post: { comments_attributes: [{ content: 'Updated comment' }] } }
C{ post: { comments_attributes: { id: 5, content: 'Updated comment' } } }
D{ post: { comments_attributes: [{ id: 5, content: 'Updated comment' }] } }
Attempts:
2 left
💡 Hint

Remember that nested attributes for updating use a hash with keys as string ids.

🔧 Debug
advanced
2:00remaining
Why does this nested attribute update fail silently?

Given the following code, why does updating the nested profile of a User not change the profile's name?

Ruby on Rails
class User < ApplicationRecord
  has_one :profile
  accepts_nested_attributes_for :profile
end

user = User.find(1)
user.update(profile_attributes: { id: user.profile.id, name: 'New Name' })
user.profile.name
AThe profile's name is not updated because the <code>profile</code> association is not marked as <code>autosave: true</code>
BThe profile's name is not updated because <code>profile_attributes</code> is missing <code>allow_destroy: true</code>
CThe profile's name is not updated because the <code>id</code> key is missing in the nested attributes
DThe profile's name is not updated because the <code>profile</code> model has validations that fail silently
Attempts:
2 left
💡 Hint

Think about what Rails needs to save changes to associated records.

state_output
advanced
2:00remaining
What is the state of child records after nested attributes update with _destroy flag?

Given a Project with many tasks and accepts_nested_attributes_for :tasks, allow_destroy: true, what happens after this update?

Ruby on Rails
project = Project.find(1)
project.update(tasks_attributes: [{ id: 10, _destroy: '1' }])
remaining_tasks = project.tasks.pluck(:id)
AThe update fails because _destroy must be a boolean true, so remaining_tasks includes 10
BThe task with id 10 is deleted, so remaining_tasks does not include 10
CThe task with id 10 is marked as completed but not deleted, so remaining_tasks includes 10
DThe task with id 10 is archived but still present, so remaining_tasks includes 10
Attempts:
2 left
💡 Hint

Recall what the _destroy flag does in nested attributes.

🧠 Conceptual
expert
3:00remaining
Why use nested attributes instead of manual child record management?

Which is the best explanation for why Rails provides accepts_nested_attributes_for to handle nested attributes?

AIt enforces database-level foreign key constraints to prevent orphaned child records
BIt automatically validates all child records without needing explicit validation rules in child models
CIt simplifies form handling by allowing parent and child records to be created or updated in one step, reducing manual code and errors
DIt replaces the need for any callbacks or custom logic in models managing associations
Attempts:
2 left
💡 Hint

Think about the developer experience when working with forms that include related data.