has_many 关系的 ActiveAdmin 表单不允许您创建,因为父级不存在
ActiveAdmin form for has_many relationship won't let you create because the parent does not exist
我有 Table1 与 Table2
有 has_many 关系
# app/models/table1.rb
class Table1 < ApplicationRecord
.
.
.
has_many :table2_relations, class_name: Table2, foreign_key: instance2_id
表2中的belongs_to关系标记为必填:
# app/models/table2.rb
class Table2 < ApplicationRecord
.
.
.
belongs_to instance2, class_name: Table1, required: true
我的 ActiveAdmin 文件是:
ActiveAdmin.register Table1 do
.
.
.
controller do
def create
.
.
.
end
我有一个 ActiveAdmin 页面来创建一个新的 Table1,其中还包括 table2_relations,但是由于 belongs_to 要求为真,当调用 super 时,它将首先执行所有插入语句然后提交,导致instance2 不存在错误,因为在提交 table2_relations 的创建之前尚未提交 Table1 的创建。 table2_relations 的创建只能在更新的上下文中进行。如何在创建 table2_relations 之前首先提交 Table1 的创建?谢谢
请参阅下面的标题 'Nested Resources' link:
Link: https://activeadmin.info/5-forms.html
尝试创建嵌套表单。这将解决问题。
我一直按照Pragya说的做Nested Resources,但是不行。然后我发现我必须在 Table1 中添加 inverse_of: :instance2 就像这个答案一样:ActiveAdmin has_many form not saved if parent model is new and is NOT NULL in child model
我有 Table1 与 Table2
有 has_many 关系# app/models/table1.rb
class Table1 < ApplicationRecord
.
.
.
has_many :table2_relations, class_name: Table2, foreign_key: instance2_id
表2中的belongs_to关系标记为必填:
# app/models/table2.rb
class Table2 < ApplicationRecord
.
.
.
belongs_to instance2, class_name: Table1, required: true
我的 ActiveAdmin 文件是:
ActiveAdmin.register Table1 do
.
.
.
controller do
def create
.
.
.
end
我有一个 ActiveAdmin 页面来创建一个新的 Table1,其中还包括 table2_relations,但是由于 belongs_to 要求为真,当调用 super 时,它将首先执行所有插入语句然后提交,导致instance2 不存在错误,因为在提交 table2_relations 的创建之前尚未提交 Table1 的创建。 table2_relations 的创建只能在更新的上下文中进行。如何在创建 table2_relations 之前首先提交 Table1 的创建?谢谢
请参阅下面的标题 'Nested Resources' link:
Link: https://activeadmin.info/5-forms.html
尝试创建嵌套表单。这将解决问题。
我一直按照Pragya说的做Nested Resources,但是不行。然后我发现我必须在 Table1 中添加 inverse_of: :instance2 就像这个答案一样:ActiveAdmin has_many form not saved if parent model is new and is NOT NULL in child model