0

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
  • Can you share your `list_params`? – Thang Feb 24 '20 at 11:10
  • Properly because `@list.items = items` has changed the `items` then later you still want to update the attributes of those items in `if @list.update(list_params)` – Thang Feb 24 '20 at 11:16
  • Thanks I added the list_params in the question above. @list.items = items was added afterwards, but if I remove it, I am getting the same error. – benjamin ratelade Feb 24 '20 at 11:20

1 Answers1

0

@list.items = items mean list has many item, and items table have list_id column.

You can try @list.update item_ids: list_params[:items_attributes][:id]

Thuy Nguyen
  • 342
  • 2
  • 8
  • Thanks for the quick answer. My models are actually in a "has_and_belongs_to_many" association, so ```item``` doesn't have a ```list_id```. Instead, the association is done through a joining table ```list_items``` which has a ```list_id``` and ```item_id``` for each record. – benjamin ratelade Feb 24 '20 at 11:12