Rails5在更改联接表中attribute的值时插入而不是更新



我有一个使用has_many的HABTM关系:通过关联。我在更新联接表中的属性时遇到了问题,因为它没有更新记录,而是在表中插入一个新记录,从而创建重复记录。

在创建索引和添加验证时,我尝试使用UNIQUE约束,但现在当我更新记录时,会出现验证错误或错误:BatchesController#更新中的ActiveRecord::RecordNotUniqueMysql2::错误:重复条目

提供一些上下文:

  • 我有4个表:manufacturing_orders、order_products、batches和batches_order_products(联接表(
  • 一个manufacturing_order有许多order_products
  • 制造订单也有许多批次
  • 批次有许多order_products
  • 当我创建一个批次时,我会复制属于同一个manufacturing_order的所有order_products,并在表格中为每个订单分配一个数量

所以创建看起来很好,但当我更新任何数量时,它只是再次插入整个关系,而不是更新现有关系

型号manufacturing_order.rb:

class ManufacturingOrder < ApplicationRecord
has_many :batches, inverse_of: :manufacturing_order, dependent: :destroy
has_many :order_products, inverse_of: :manufacturing_order, dependent: :destroy
accepts_nested_attributes_for :order_products
accepts_nested_attributes_for :batches
end

型号order_product.rb:

class OrderProduct < ApplicationRecord
belongs_to :manufacturing_order
has_many :batches_order_products
has_many :batches, :through => :batches_order_products
end

型号批次.rb:

class Batch < ApplicationRecord
belongs_to :manufacturing_order
has_many :batches_order_products
has_many :order_products, :through => :batches_order_products
accepts_nested_attributes_for :batches_order_products
end

型号批次订单产品.rb:

class BatchesOrderProduct < ApplicationRecord
belongs_to :batch
belongs_to :order_product
validates :batch_id, uniqueness: { scope: :order_product_id }
end

控制器batches_Controller.rb:

class BatchesController < ApplicationController
def new
manufacturing_order = ManufacturingOrder.find(params[:manufacturing_order_id])
order_products = manufacturing_order.order_products
@batch = Batch.new({
manufacturing_order: manufacturing_order,
order_products: order_products
})
end
def create
@batch = Batch.new(load_params)
if @batch.save
flash[:notice] = crud_success
redirect_to action: :index
else
flash[:error] = @batch.errors.full_messages.to_sentence
render action: :new
end
end
def edit
@batch = Batch.find(params[:id])
end
def update
@batch = Batch.find(params[:id])
if @batch.update_attributes(load_params)
flash[:notice] = crud_success
redirect_to action: :index
else
flash[:error] = @batch.errors.full_messages.to_sentence
render action: :edit
end
end
private
def load_params
params.require(:batch)
.permit(:name,
:date,
:manufacturing_order_id,
:status,
order_products: [],
order_products_ids: [],
batches_order_products_attributes: [:id, :quantity, :order_product_id]
)
end
end

这是批量形式:

= bootstrap_form_for([@batch.manufacturing_order, @batch]) do |f|
= f.hidden_field :manufacturing_order_id
= f.text_field :name, label: 'Name'
= f.text_field :date
table
thead
tr
th= "Product"
th= "Quantity"
tbody
= f.fields_for :batches_order_products do |bop|
= bop.hidden_field :order_product_id
tr
td
= bop.object.order_product.name
td
= bop.text_field :quantity
= f.submit 'Save'

任何帮助都将不胜感激。谢谢

更新:这些是提交编辑表单时传递的参数。有线索吗?

{"utf8"=>"✓",
"_method"=>"patch",
"batch"=>
{"manufacturing_order_id"=>"8",
"name"=>"MAS",
"date"=>"07/05/2020",
"batches_order_products_attributes"=>
{"0"=>{"order_product_id"=>"12", "quantity"=>"77777777", "id"=>""},
"1"=>{"order_product_id"=>"13", "quantity"=>"9.0", "id"=>""},
"2"=>{"order_product_id"=>"14", "quantity"=>"7.0", "id"=>""}}},
"commit"=>"Guardar",
"manufacturing_order_id"=>"8",
"id"=>"7"}

编辑2:我更新了嵌套表单,将id包含在一个隐藏字段中,如下所示:

= f.fields_for :batches_order_products do |bop|
= bop.hidden_field :order_product_id
= bop.hidden_field :id, value: @batch.id
= bop.object.order_product.name
= bop.text_field :quantity, label: ''

但现在Rails在更新时抱怨这一点:

ActiveRecord::StatementInvalid in BatchesController#update
Mysql2::Error: Unknown column 'batches_order_products.' in 'where clause': SELECT `batches_order_products`.* FROM `batches_order_products` WHERE `batches_order_products`.`batch_id` = 9 AND `batches_order_products`.`` IN ('9', '9', '9', '9', '9') 

我不知道Rails为什么在SQL查询中添加最后一个奇怪的部分。

所以我终于弄明白了。

问题是联接表需要一个ID列来引用。该表的索引为batch_ID和order_product_ID,但由于某种原因,它不起作用,ActiveRecord正在寻找ID。添加它解决了问题。

感谢@max给了我们一些分数。

class AddIndexToBatchesOrderProductsJoinTable < ActiveRecord::Migration[5.2]
def change
# add_index :batches_order_products, [:batch_id, :order_product_id], unique: true
add_column :batches_order_products, :id, :primary_key
end
end

最新更新