Ruby on rails 如何在多个模型之间建立正确的活动记录关联
我试着制作产品目录。我不明白如何正确地联想 这是我的schema.rb:Ruby on rails 如何在多个模型之间建立正确的活动记录关联,ruby-on-rails,Ruby On Rails,我试着制作产品目录。我不明白如何正确地联想 这是我的schema.rb: create_table "products", force: :cascade do |t| t.string "name", limit: 50, null: false t.integer "product_type_id" end create_table "product_types", force: :cascade do |t| t.string
create_table "products", force: :cascade do |t|
t.string "name", limit: 50, null: false
t.integer "product_type_id"
end
create_table "product_types", force: :cascade do |t|
t.string "name", limit: 20, null: false
end
create_table "product_specs", force: :cascade do |t|
t.string "name", limit: 20, null: false
t.string "value_type", limit: 20
end
create_table "product_specs_types", id: false, force: :cascade do |t|
t.integer "product_type_id", null: false
t.integer "product_spec_id", null: false
end
add_index "product_specs_types", ["product_type_id", "product_spec_id"], name: "type_spec_uniq", unique: true
create_table "product_spec_keys", force: :cascade do |t|
t.integer "value_id", null: false
t.integer "product_id"
t.integer "product_spec_id", null: false
end
create_table "spec_value_booleans", force: :cascade do |t|
t.boolean "value", limit: 20, null: false
end
create_table "spec_value_numbers", force: :cascade do |t|
t.integer "value", limit: 20, null: false
end
create_table "spec_value_strings", force: :cascade do |t|
t.string "value", limit: 20, null: false
end
产品
是目录项
产品类型
告诉产品它有什么产品规格
产品规格类型
只需连接表
spec\u value\uVT是用于保存不同类型变量的表格
product\u spec\u key
是(产品及其规格)和spec\u value\u其中一个之间关系的表。polymorphy type key是product\u type.value\u id
。我不知道如何正确使用它,因为它必须同时填写所有三列
产品规格类型
通过产品规格类型
产品
有许多产品规格
到产品规格类型
-工作正常(使用产品类型id
类似键)
但如何建立这样的关系:
@product.product_specs.each do |spec|
spec.value = 'something'
p spec.value
end
其中,spec.value
-是来自spec\u value\u
VT(VT-是spec.value\u类型)我真的很确定你想用它实现什么。你想解决的问题是什么?我无法解释,因为我的英语不好((如果你无法解释,我们也无能为力):(尝试。我想我只是糊涂了。也许我需要展示所有有问题的模型和schema.rb?是的,我想那最好