Nested attributes allow you to save attributes on associated records through the parent. This means you can create or update child records directly when saving the parent record.
Click to reveal answer
beginner
Which method do you use in a Rails model to accept nested attributes for an associated model?
You use accepts_nested_attributes_for :association_name in the parent model to enable nested attributes for the associated child model.
Click to reveal answer
beginner
How do nested attributes help in a form with parent and child models?
Nested attributes let you build a single form that updates both the parent and its children at once. For example, a form for a <em>Project</em> can also create or update its <em>Tasks</em> inside the same form.
Click to reveal answer
intermediate
What option can you add to accepts_nested_attributes_for to allow deleting child records through the parent form?
You can add <code>allow_destroy: true</code> to let the form mark child records for deletion when saving the parent.
Click to reveal answer
intermediate
What is a common security practice when using nested attributes in Rails forms?
Use strong parameters in the controller to whitelist nested attributes. This prevents unwanted or malicious data from being saved.
Click to reveal answer
Which method enables nested attributes in a Rails model?
Aaccepts_nested_attributes_for
Bhas_nested_attributes
Cnested_attributes_enabled
Denable_nested_attributes
✗ Incorrect
The correct method is accepts_nested_attributes_for to allow nested attributes for an association.
What does the option allow_destroy: true do in nested attributes?
ADisables nested attributes
BPrevents child records from being updated
CAutomatically creates child records
DAllows child records to be deleted through the parent form
✗ Incorrect
Setting allow_destroy: true lets you delete child records by marking them for destruction in the parent form.
In a form using nested attributes, how do you name the fields for child records?
Hint: Think: parent model saving child data together [OK]
Common Mistakes:
Confusing it with database table creation
Assuming it validates nested attributes automatically
Thinking it generates controller code
2. Which of the following is the correct syntax to enable nested attributes for a child model named tasks inside a parent model?
easy
A. accepts_nested_attributes_for :tasks
B. accepts_nested_attributes_for :task
C. accepts_nested_attributes_for tasks
D. accepts_nested_attributes_for 'tasks'
Solution
Step 1: Identify the correct argument type
The method expects a symbol representing the association name, which is plural if the association is has_many.
Step 2: Match the association name
Since the child model is tasks (plural), the correct syntax is accepts_nested_attributes_for :tasks.
Final Answer:
accepts_nested_attributes_for :tasks -> Option A
Quick Check:
Use symbol with plural association name [OK]
Hint: Use symbol and plural for has_many associations [OK]
Common Mistakes:
Using singular instead of plural for has_many
Passing string instead of symbol
Omitting colon before association name
3. Given the following models and controller snippet, what will happen when creating a Project with nested tasks_attributes?
class Project < ApplicationRecord
has_many :tasks
accepts_nested_attributes_for :tasks
end
class Task < ApplicationRecord
belongs_to :project
end
# In ProjectsController
params = { project: { name: 'Build App', tasks_attributes: [{ title: 'Design UI' }, { title: 'Write Tests' }] } }
@project = Project.new(params[:project])
@project.save
medium
A. A new project and two associated tasks will be saved in the database.
B. Only the project will be saved; tasks will be ignored.
C. An error will occur because nested attributes are not permitted in the controller.
D. The project will save but tasks will be saved without association.
Solution
Step 1: Check model setup for nested attributes
The Project model uses accepts_nested_attributes_for :tasks, allowing nested task attributes.
Step 2: Analyze controller params and save
The params include tasks_attributes with two tasks. Calling Project.new with these params and then save will save the project and its tasks together.
Final Answer:
A new project and two associated tasks will be saved in the database. -> Option A
Quick Check:
Nested attributes save parent and children together [OK]
Hint: Nested attributes save parent and children in one go [OK]
Common Mistakes:
Assuming tasks are ignored without explicit permit
Thinking save fails without controller strong params shown
Believing tasks save without association
4. Identify the error in the following controller code when trying to save nested attributes:
def project_params
params.require(:project).permit(:name, tasks_attributes: [:title])
end
@project = Project.new(project_params)
@project.save
Assuming the models are set up correctly with accepts_nested_attributes_for :tasks.
medium
A. The require method should be permit instead.
B. The accepts_nested_attributes_for should be in the controller, not the model.
C. The tasks_attributes key should be a string, not a symbol.
D. The permit method is missing the :id key inside tasks_attributes.
Solution
Step 1: Understand strong parameters for nested attributes
When updating nested records, Rails expects the :id key to identify existing child records.
Step 2: Check the permit call
The permit call only allows :title inside tasks_attributes, missing :id. This causes update failures or errors.
Final Answer:
The permit method is missing the :id key inside tasks_attributes. -> Option D
Quick Check:
Permit :id for nested updates [OK]
Hint: Permit :id inside nested attributes for updates [OK]
Common Mistakes:
Omitting :id in nested strong params
Confusing require and permit methods
Placing accepts_nested_attributes_for in controller
5. You have a Survey model with many questions. You want to allow users to add, update, and remove questions through the survey form. Which combination of code is needed to correctly handle nested attributes and allow question removal?
hard
A. In Survey model: accepts_nested_attributes_for :questions, reject_if: :all_blank
In controller: permit questions_attributes with :content, :_destroy
B. In Survey model: accepts_nested_attributes_for :questions, allow_destroy: true
In controller: permit questions_attributes with :id, :content, :_destroy
C. In Survey model: has_many :questions
In controller: permit questions_attributes with :id, :content
D. In Survey model: accepts_nested_attributes_for :questions
In controller: permit questions_attributes with :content only
Solution
Step 1: Enable nested attributes with destroy option
To allow removal of nested records, accepts_nested_attributes_for must include allow_destroy: true.
Step 2: Permit necessary keys in controller
The controller must permit :id to identify records, :content for data, and :_destroy to mark records for deletion.
Final Answer:
In Survey model: accepts_nested_attributes_for :questions, allow_destroy: true
In controller: permit questions_attributes with :id, :content, :_destroy -> Option B
Quick Check:
allow_destroy and permit :_destroy for removals [OK]
Hint: Use allow_destroy and permit :_destroy to remove nested records [OK]
Common Mistakes:
Forgetting allow_destroy in model
Not permitting :_destroy in strong params
Permitting only :content without :id
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.