Ruby on rails 如果存在则更新,如果为空则销毁,如果不存在则创建';数组表单提交中不存在
我试图一次更新多个记录,但我的唯一性范围验证出现问题 当我在每个记录的基础上创建一个记录时,它会按照标题的要求执行。但对于数组,它不会 为什么?? 因为参数不传递要更新的嵌套属性的ID 每个记录与阵列上更新的参数差异示例: Mutli更新:Ruby on rails 如果存在则更新,如果为空则销毁,如果不存在则创建';数组表单提交中不存在,ruby-on-rails,Ruby On Rails,我试图一次更新多个记录,但我的唯一性范围验证出现问题 当我在每个记录的基础上创建一个记录时,它会按照标题的要求执行。但对于数组,它不会 为什么?? 因为参数不传递要更新的嵌套属性的ID 每个记录与阵列上更新的参数差异示例: Mutli更新: Parameters: {"utf8"=>"✓", "authenticity_token"=>"==", "shop_product_ids"=>["42"], "shop_product"=>{"shop_product_prin
Parameters: {"utf8"=>"✓", "authenticity_token"=>"==", "shop_product_ids"=>["42"], "shop_product"=>{"shop_product_print_files_attributes"=>{"0"=>{"print_location_id"=>"1", "image_file_id"=>"2"}, "1"=>{"print_location_id"=>"2", "image_file_id"=>"2"}, "2"=>{"print_location_id"=>"3", "image_file_id"=>""}, "3"=>{"print_location_id"=>"4", "image_file_id"=>""}, "4"=>{"print_location_id"=>"5", "image_file_id"=>""}, "5"=>{"print_location_id"=>"6", "image_file_id"=>""}}}, "commit"=>"Edit Checked"}
1记录更新:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"==", "shop_product"=>{... "shop_product_print_files_attributes"=>{"0"=>{"print_location_id"=>"1", "image_file_id"=>"1", "id"=>"145"}, "1"=>{"print_location_id"=>"2", "image_file_id"=>"", "id"=>"151"}, "2"=>{"print_location_id"=>"3", "image_file_id"=>""}, "3"=>{"print_location_id"=>"4", "image_file_id"=>""}, "4"=>{"print_location_id"=>"5", "image_file_id"=>""}, "5"=>{"print_location_id"=>"6", "image_file_id"=>""}},"id"=>"42"}, "commit"=>"Sync", "id"=>"42"}
在一次记录更新时,它会传递车间\产品\打印\文件ID以进行更新。在multi上,它不会也不应该
目标:如果存在则更新,如果不为空则创建,如果为空则销毁。我需要以某种方式做我在控制器中的模型拒绝\u中所做的事情,但我对如何做感到困惑
型号:
class PrintLocation < ApplicationRecord
has_many :shop_products, through: :shop_product_print_files
has_many :shop_product_print_files
end
class ShopProductPrintFile < ApplicationRecord
validates :shop_product, uniqueness: { scope: :print_location }
belongs_to :shop_product
belongs_to :print_location
belongs_to :image_file
end
class ShopProduct < ApplicationRecord
has_many :shop_product_print_files
has_many :print_locations, through: :shop_product_print_files
accepts_nested_attributes_for :shop_product_print_files
...
accepts_nested_attributes_for :shop_product_print_files, reject_if: :reject_file, :allow_destroy => true
def reject_file(attributes)
if attributes[:image_file_id].blank?
if attributes[:id].present?
attributes.merge!({:_destroy => 1}) && false
else
true
end
end
end
end
class ImageFile < ApplicationRecord
# this is where users upload files to
belongs_to :user
has_many :shop_product_print_files
end
在逐个记录更新时,:reject_file
方法在通过的SPPF的帮助下处理我的目标。但在对数组执行此操作时,没有通过SPPF ID,这会导致错误:
> ActiveRecord::RecordInvalid (Validation failed: Shop product print
> files shop product has already been taken):
有人对我如何做到这一点有什么结论吗
我省略了更新数组的表单以保持简短,因为参数应该足够解释它,因为我需要的很可能是控制器条件语句来实现我的目标。我认为您误解了拒绝代码>方法。is所做的是迭代数组,将每个元素传递到块中,并返回块返回false的所有元素。您现在使用它的方式似乎是试图将@shop\u products
设置为所有无法更新的记录,除非使用update\u属性代码>你让它无论如何都会抛出一个错误。另外,您使用update\u attributes
而不是普通的update
有什么原因吗?没有原因,我实际上遵循了有关此的教程,因为我以前从未这样做过,。我尝试过使用更新(强参数)处理相同的问题吗?我想它也会以同样的方式工作,但没有。但是是的。为了进一步重复我的目标,这只是为了更新@shop\u product嵌套的\u属性,实际上,ShopProduct模型不应该进行任何更新或更改,只有SPPF模型应该认为您误解了拒绝代码>方法。is所做的是迭代数组,将每个元素传递到块中,并返回块返回false的所有元素。您现在使用它的方式似乎是试图将@shop\u products
设置为所有无法更新的记录,除非使用update\u属性代码>你让它无论如何都会抛出一个错误。另外,您使用update\u attributes
而不是普通的update
有什么原因吗?没有原因,我实际上遵循了有关此的教程,因为我以前从未这样做过,。我尝试过使用更新(强参数)处理相同的问题吗?我想它也会以同样的方式工作,但没有。但是是的。为了进一步迭代我的目标,这仅用于更新@shop\u product嵌套的\u属性,实际上,ShopProduct模型不应该进行任何更新或更改,只有SPPF模型应该进行更新或更改
> ActiveRecord::RecordInvalid (Validation failed: Shop product print
> files shop product has already been taken):