Rails 5: update attribute in 有很多through join table
Rails 5: Update attribute in has many through join table
我目前正在网上商店工作,我正在使用 Rails 5. 我有以下模型和关系:
# Products
class Product < ApplicationRecord
has_many :product_variants
has_many :variants, through: :product_variants
...
class Variant < ApplicationRecord
has_many :product_variants
has_many :products, through: :product_variants
...
# My join table between Product and Variant
class ProductVariant < ApplicationRecord
belongs_to :price, optional: true
belongs_to :product, optional: true
belongs_to :variant, optional: true
我的 ProductVariant
模型有一个我想更新的附加属性table,t.integer "quantity"
。在 form_for
中更新此属性时,我有点不知所措。
我当前的表单解决方案如下所示(我使用 HAML):
= form_for @product, html: { method: :patch } do |product_builder|
= product_builder.fields_for :product_variants, product_variant do |variant_builder|
= variant_builder.hidden_field :variant_id, value: product_variant.variant.id
= variant_builder.number_field :quantity
= variant_builder.submit
但它不能正常工作。如果连接 table 有一个 ID 列,感觉会更容易,但我想这在数据库中是多余的。当我单击提交按钮时,它目前似乎创建了 ProductVariant
的另一个实例。
知道我应该如何正确更新我的 table 中的 quantity
属性吗?
完成
这是我点击提交时记录的内容:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uisxjfvj9LxZVJWcDVos66tJWNfHkld5+/gdfGv1D2WZsrLJcH7KLxb5eSDAYIL23mEEho18Pv/53bSEP8cC9A==", "product"=>{"product_variants_attributes"=>{"0"=>{"variant_id"=>"1", "product_id"=>"11", "quantity"=>"20", "id"=>""}}}, "commit"=>"Update Product variant", "id"=>"11"}
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 11], ["LIMIT", 1]]
Unpermitted parameter: :id
(0.1ms) begin transaction
Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]]
Variant Load (0.2ms) SELECT "variants".* FROM "variants" WHERE "variants"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Brand Exists (0.2ms) SELECT 1 AS one FROM "brands" WHERE "brands"."name" = ? AND ("brands"."id" != ?) LIMIT ? [["name", "Fredric Malle"], ["id", 4], ["LIMIT", 1]]
SQL (0.4ms) INSERT INTO "product_variants" ("variant_id", "product_id", "quantity") VALUES (?, ?, ?) [["variant_id", 1], ["product_id", 11], ["quantity", 20]]
(2.7ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "note_parts" INNER JOIN "product_note_parts" ON "note_parts"."id" = "product_note_parts"."note_part_id" WHERE "product_note_parts"."product_id" = ? [["product_id", 11]]
Redirected to http://localhost:3000/products/11/edit
Completed 302 Found in 15ms (ActiveRecord: 4.0ms)
您正在做 has_many :though
。在这种情况下,连接 table 需要有一个主键(通常是 :id)。否则你不能像你在这里想要的那样直接访问连接 table 。
另一方面,如果您不需要直接访问联接 table(例如那里没有额外的列),那么您可以设置一个 has_and_belongs_to_many,没有 id 列对于加入 table.
我目前正在网上商店工作,我正在使用 Rails 5. 我有以下模型和关系:
# Products
class Product < ApplicationRecord
has_many :product_variants
has_many :variants, through: :product_variants
...
class Variant < ApplicationRecord
has_many :product_variants
has_many :products, through: :product_variants
...
# My join table between Product and Variant
class ProductVariant < ApplicationRecord
belongs_to :price, optional: true
belongs_to :product, optional: true
belongs_to :variant, optional: true
我的 ProductVariant
模型有一个我想更新的附加属性table,t.integer "quantity"
。在 form_for
中更新此属性时,我有点不知所措。
我当前的表单解决方案如下所示(我使用 HAML):
= form_for @product, html: { method: :patch } do |product_builder|
= product_builder.fields_for :product_variants, product_variant do |variant_builder|
= variant_builder.hidden_field :variant_id, value: product_variant.variant.id
= variant_builder.number_field :quantity
= variant_builder.submit
但它不能正常工作。如果连接 table 有一个 ID 列,感觉会更容易,但我想这在数据库中是多余的。当我单击提交按钮时,它目前似乎创建了 ProductVariant
的另一个实例。
知道我应该如何正确更新我的 table 中的 quantity
属性吗?
完成
这是我点击提交时记录的内容:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uisxjfvj9LxZVJWcDVos66tJWNfHkld5+/gdfGv1D2WZsrLJcH7KLxb5eSDAYIL23mEEho18Pv/53bSEP8cC9A==", "product"=>{"product_variants_attributes"=>{"0"=>{"variant_id"=>"1", "product_id"=>"11", "quantity"=>"20", "id"=>""}}}, "commit"=>"Update Product variant", "id"=>"11"}
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 11], ["LIMIT", 1]]
Unpermitted parameter: :id
(0.1ms) begin transaction
Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]]
Variant Load (0.2ms) SELECT "variants".* FROM "variants" WHERE "variants"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Brand Exists (0.2ms) SELECT 1 AS one FROM "brands" WHERE "brands"."name" = ? AND ("brands"."id" != ?) LIMIT ? [["name", "Fredric Malle"], ["id", 4], ["LIMIT", 1]]
SQL (0.4ms) INSERT INTO "product_variants" ("variant_id", "product_id", "quantity") VALUES (?, ?, ?) [["variant_id", 1], ["product_id", 11], ["quantity", 20]]
(2.7ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "note_parts" INNER JOIN "product_note_parts" ON "note_parts"."id" = "product_note_parts"."note_part_id" WHERE "product_note_parts"."product_id" = ? [["product_id", 11]]
Redirected to http://localhost:3000/products/11/edit
Completed 302 Found in 15ms (ActiveRecord: 4.0ms)
您正在做 has_many :though
。在这种情况下,连接 table 需要有一个主键(通常是 :id)。否则你不能像你在这里想要的那样直接访问连接 table 。
另一方面,如果您不需要直接访问联接 table(例如那里没有额外的列),那么您可以设置一个 has_and_belongs_to_many,没有 id 列对于加入 table.