I am trying to assign a collection of existing records to an existing (or new) associated record.
For example:
class List < ApplicationRecord
has_and_belongs_to_many :items, join_table: :list_items
accepts_nested_attributes_for :items
end
class Item < ApplicationRecord
has_and_belongs_to_many :lists, join_table: :list_items
end
From my view for the List's Create or Edit forms, I am sending :name as well as :items_attributes with an :id for each record I want to associate to my List.
In my Lists controller, I do:
def update
items = Item.where(id: list_params[:items_attributes][:id])
@list.items = items
respond_to do |format|
// the following line breaks, because @list currently has no "item" (the
// association built previously hasn't been saved yet)
if @list.update(list_params)
format.html { redirect_to @list, notice: 'List was successfully updated.' }
format.json { render :show, status: :ok, location: @list }
else
format.html { render :edit }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
However, I am getting
ActiveRecord::RecordNotFound in ListsController#update
Couldn't find Item with ID=1 for List with ID=1
How would I go about saving this association in a "Rails" way?
EDIT
list_params
would be something like this (using Cocoon gem):
{"name"=>"Standard location shoot", "items_attributes"=><ActionController::Parameters {"1582459909419"=><ActionController::Parameters {"id"=>"1"} permitted: true>} permitted: true>}
Unfiltered parameters are:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"qkXKM9U/1+Y8T4RttvPg==", "list"=>{"name"=>"Standard location shoot", "items_attributes"=>{"1582459152520"=>{"id"=>"1", "_destroy"=>"false"}}}, "commit"=>"Update List", "controller"=>"lists", "action"=>"update", "id"=>"1"}
And more generally, the definition for list_params
is:
# Never trust parameters from the scary internet, only allow the white list through.
def list_params
params.fetch(:list, {}).permit(:name, items_attributes: [:id])
end