Rails API多对多关联下提交JSON如何保存中间表额外字段
问题根因
你当前报错的核心原因是关联声明与嵌套属性的指向不匹配:你配置了accepts_nested_attributes_for :carray,但Rails默认将carray识别为C模型的关联,因此你提交的number字段会被尝试写入C模型,而number是中间表B的字段,自然就抛出属性不存在的错误。
修复方案
1. 修正模型关联配置
首先在A模型中显式声明对中间表B的关联,再嵌套中间表的属性:
class A < ApplicationRecord # 先定义对中间表B的关联,此处保留你使用的carray作为别名 has_many :carray, class_name: 'B' has_many :cs, through: :carray, class_name: 'C' # 嵌套中间表的属性,此时carray指向的是B模型,number字段就会写入中间表 accepts_nested_attributes_for :carray end # B模型保持原有关联逻辑即可,可按需补充外键声明 class B < ApplicationRecord belongs_to :a, class_name: 'A' belongs_to :c, class_name: 'C' end class C < ApplicationRecord has_many :bs, class_name: 'B' has_many :as, through: :bs, class_name: 'A' end
注:如果遵循Rails默认命名规范,关联名使用小写复数(比如中间表关联叫
bs,C的关联叫cs),可以省略class_name声明,代码会更简洁。
2. 匹配参数命名
你当前JSON请求里的外键字段是id_c,和强参数里配置的c_id不匹配,两种修正方式二选一:
- 方式一:调整JSON请求的字段名,和强参数保持一致
{ "name" : "A name", "carray_attributes": [ { "c_id": "3", "number": "23" }, { "c_id": "3", "number": "15" } ] }
- 方式二:调整强参数和B模型配置,适配现有JSON结构
# 控制器强参数调整 def a_params params.require(:a).permit(:name, carray_attributes: [:id_c, :number]) end
# B模型补充外键声明 class B < ApplicationRecord belongs_to :a, class_name: 'A' belongs_to :c, class_name: 'C', foreign_key: 'id_c' end
调整完成后再提交请求,number字段就会正确写入中间表B中。
内容的提问来源于stack exchange,提问作者N0ne
相关产品推荐
相关产品推荐

