ruby-on-rails - Rails 6嵌套属性,销毁记录的唯一性验证失败
问题描述
我在验证嵌套属性的唯一性时遇到了问题。我有 2 个模型
#model : CompanyStore
class CompanyStore < ApplicationRecord
has_many :company_stores_brands, dependent: :destroy
accepts_nested_attributes_for :company_stores_brands, allow_destroy: true
end
class CompanyStoreBrand < ApplicationRecord
validates_uniqueness_of :brand_id
end
brand_id 对于CompanyStoreBrand应该是唯一的。如果我从一家商店删除品牌并分配给另一家商店,则会引发异常,因为它没有被破坏。以下是我发送的参数
"company_stores_attributes"=>[
{"id"=>1, "store_id"=>446, "company_stores_brands_attributes"=>[{"id"=>1, "_destroy"=>1, "brand_id"=>69258}]},
{"id"=>3, "store_id"=>472, "company_stores_brands_attributes"=>[{"brand_id"=>69258}]}]
params.require(:company).permit(company_stores_attributes: [:id, :store_id, :_destroy,{ company_stores_brands_attributes: %i[id brand_id _destroy] }])
我在 stackoverflow 上看到了很多解决方案,但其中一些效率低下,或者不支持此标准。有没有其他方法可以实现这一目标?
解决方案
推荐阅读
- java - 我如何将多个 json 对象附加到 json 文件的末尾
- python - 我将如何将此 txt 文件解析为 csv?
- git - 如何递归地将更改还原到 git 中的子树?
- ios - 在 xcode 11.1 上为 iOS 13 编译 React Native 的问题
- go - 尝试在新创建的目录中创建文件时出现“权限被拒绝”?
- unity3d - 在 Unity 中设置 Google Play 游戏时,我不断收到 Invalid Classname: Ambiguous match found 错误
- c# - 统一更新 Update() 中的属性值时的性能和优化
- python - AJAX 网页抓取时未接收到所有文本/json 数据
- acumatica - 寻求帮助使用 VFP 为 acumatica 开发 vfp API
- ios - 为什么需要将配置文件设置为用于调试和发布的分发?