简体   繁体   English

批量分配现有记录轨道的集合

[英]Mass assign a collection of existing records rails

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.从我对列表的创建或编辑表单的看法来看,我发送 :name 和 :items_attributes 以及我想要关联到我的列表的每条记录的 :id。

In my Lists controller, I do:在我的 Lists 控制器中,我这样做:

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?我将如何以“Rails”方式保存此关联?

EDIT编辑

list_params would be something like this (using Cocoon gem): list_params将是这样的(使用 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"}未过滤的参数有: {"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:更一般地说, list_params的定义是:

# 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

@list.items = items mean list has many item, and items table have list_id column. @list.items = items表示列表有很多项,items 表有 list_id 列。

You can try @list.update item_ids: list_params[:items_attributes][:id]你可以试试@list.update item_ids: list_params[:items_attributes][:id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM