我有一个使用has_many :through关联的HABTM关系。我在更新联接表中的属性时遇到了问题,因为它没有更新记录,只是在表中插入了一条新记录,造成了重复。
我尝试在创建索引和添加验证时使用UNIQUE约束,但现在当我更新记录时,我得到一个验证错误或错误。ActiveRecord::RecordNotUnique in BatchesController#updateMysql2::Error: 重复的条目
为了提供一些背景资料,我有4张表:manufacturing_orders、order_products、batches和batches_order_products(连接表)。
因此,创建看起来工作正常,但当我更新任何数量时,它只是再次插入整个关系,而不是更新现有的一个。
模型 制造_订单.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
型号 batch.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
这是batches的表单。
= 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'
任何帮助将是非常感激的。谢谢你的帮助
UPDATE:这些是提交编辑表单时传递的参数。有什么线索吗?
{"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"}
EDIT 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