Ruby on rails Ruby Rails标记-与3个表的多对多关系-在中间(通过)表双行
我在RubyonRails(5.0)中遇到过这种情况Ruby on rails Ruby Rails标记-与3个表的多对多关系-在中间(通过)表双行,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在RubyonRails(5.0)中遇到过这种情况 许多标记具有许多条件 如果一个相同的条件下有更多的标记,则必须确定具体的百分比分区 例子 标签“工作”的条件“与代码”百分比为40 标签“已支付”的条件“与代码”百分比为60 标签“工作”的条件“计算”百分比为100 标签“搞笑”有条件“Youtube”100% 主表标签 一切正常,只有在表标记条件中,我有两个插入,因此有两行 INSERT INTO "tags" ("name", "valid_from", "valid_to",
- 许多标记具有许多条件
- 如果一个相同的条件下有更多的标记,则必须确定具体的百分比分区
- 标签“工作”的条件“与代码”百分比为40
- 标签“已支付”的条件“与代码”百分比为60
- 标签“工作”的条件“计算”百分比为100
- 标签“搞笑”有条件“Youtube”100%
INSERT INTO "tags" ("name", "valid_from", "valid_to", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "sdf"], ["valid_from", "2020-05-08 19:56:00"], ["valid_to", "2020-05-08 19:56:00"], ["created_at", "2020-05-08 19:56:55.811745"], ["updated_at", "2020-05-08 19:56:55.811745"]]
**first INSERT**
INSERT INTO "tag_conditionings" ("tag_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["tag_id", 14], ["created_at", "2020-05-08 19:56:55.843693"], ["updated_at", "2020-05-08 19:56:55.843693"]]
INSERT INTO "tag_conditions" ("condition", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["condition", "50sd"], ["created_at", "2020-05-08 19:56:55.865495"], ["updated_at", "2020-05-08 19:56:55.865495"]]
**second INSERT**
INSERT INTO "tag_conditionings" ("tag_condition_id", "tag_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["tag_condition_id", 16], ["tag_id", 14], ["created_at", "2020-05-08 19:56:55.919774"], ["updated_at", "2020-05-08 19:56:55.919774"]]
我是业余爱好者,你能帮我吗我做错了什么?我花了很多时间搜索、阅读、测试,但没有结果
多谢各位
Ivanhoe在
标记条件中增加两行的原因是,当您有一个has\u many to:
关联时,rails将在联接表中隐式创建行:
Tag.first
.tag_conditions
.create(some_attribute: 'some_value')
上述操作在tag\u条件中创建一行,在tag\u条件中创建一行
当使用嵌套属性时,同样适用:
Tag.create(
tag_conditions_attributes: [{ some_attribute: 'some_value' }]
)
class TagConditioning < ApplicationRecord
belongs_to :tag_condition, optional: true
belongs_to :tag, optional: true
accepts_nested_attributes_for :tag_conditions
end
Tag.create(
tag_conditioning_attributes: [
{
percent: 50,
tag_condition_attributes: [
{ some_attribute: 'some_value' }
]
}
]
)
如果要显式创建联接表实体和联接实体,则需要嵌套嵌套属性:
Tag.create(
tag_conditions_attributes: [{ some_attribute: 'some_value' }]
)
class TagConditioning < ApplicationRecord
belongs_to :tag_condition, optional: true
belongs_to :tag, optional: true
accepts_nested_attributes_for :tag_conditions
end
Tag.create(
tag_conditioning_attributes: [
{
percent: 50,
tag_condition_attributes: [
{ some_attribute: 'some_value' }
]
}
]
)
并通过以下方式将嵌套属性列为白名单:
def tag_attributes
params.require(:tag)
.permit(
:foo,
:bar,
tag_conditionings_attributes: [
:percent,
tag_conditions_attributes: [
:conditions
]
]
)
end
我真的不建议再深入嵌套了,因为复杂程度变得疯狂了。另一种方法是设置单独的API端点,并使用ajax调用以一种对用户无缝但实际上是原子的方式更新“嵌套”记录 你好,麦克斯,非常感谢你提供的具体信息。我试着执行你的建议。也许有些细节是错的<代码>参数.require(:tag).permit(:name,:valid_from,:valid_to,tag_conditions_attributes:[:percent,tag_conditions_attributes:[:condition]]))
不起作用,因为tag\u条件不是tag\u条件属性,而只是tag\u条件。如何从tag\u条件更改为tag\u条件属性?我不确定我是否理解你的意思。当我实现上述所有内容时,我没有插入到表tag\u条件。我只插入表tag和tag_。(byebug)tag_params Unpermitted参数:tag_条件“cccc”,“tag_条件\u属性”=>“1”}允许:true>}允许:true>}允许:true>
Tag.create(
tag_conditions_attributes: [{ some_attribute: 'some_value' }]
)
class TagConditioning < ApplicationRecord
belongs_to :tag_condition, optional: true
belongs_to :tag, optional: true
accepts_nested_attributes_for :tag_conditions
end
Tag.create(
tag_conditioning_attributes: [
{
percent: 50,
tag_condition_attributes: [
{ some_attribute: 'some_value' }
]
}
]
)
<%= form_with(model: @tag) do |f| %>
<%= f.fields_for :tag_conditionings do |conditionings| %>
<%= conditionings.number_field :percent, in: 1..100 %>
<%= conditionings.fields_for :tag_conditions do |tc| %>
<%= tc.text_field :conditions %>
<% end %>
<% end %>
# ...
<% end %>
def tag_attributes
params.require(:tag)
.permit(
:foo,
:bar,
tag_conditionings_attributes: [
:percent,
tag_conditions_attributes: [
:conditions
]
]
)
end