Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 中的Rails 5:Update属性有许多贯穿连接表_Ruby On Rails_Database_Ruby On Rails 5_Nested Forms_Has Many Through - Fatal编程技术网

Ruby on rails 中的Rails 5:Update属性有许多贯穿连接表

Ruby on rails 中的Rails 5:Update属性有许多贯穿连接表,ruby-on-rails,database,ruby-on-rails-5,nested-forms,has-many-through,Ruby On Rails,Database,Ruby On Rails 5,Nested Forms,Has Many Through,我目前在一家网店工作,使用的是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 :produ

我目前在一家网店工作,使用的是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
但它不能正常工作。如果联接表有一个ID列,感觉会更简单,但我想这在数据库中是多余的。当我单击submit按钮时,它当前似乎创建了另一个
ProductVariant
实例

您知道我应该如何正确更新联接表中的
quantity
属性吗

用于完成

这是我单击“提交”时记录的内容:

参数:{“utf8”=>“✓", "真实性令牌“=>”uisxjfvj9LxZVJWcDVos66tJWNfHkld5+/gdfGv1D2WZsrLJcH7KLxb5eSDAYIL23mEEho18Pv/53bSEP8cC9A==”,“产品”=>{“产品变体”属性“=>{“0”=>{“变体id”=>“1”,“产品id”=>“11”,“数量”=>“20”,“id”=>,“id”=>“}>,“提交”=>“更新产品变体”,“id”=>“11”}
产品负载(0.1ms)从“产品”中选择“产品”。*其中“产品”。“id”=?限制?[[“id”,11],“限制”,1]]
不允许的参数::id
(0.1ms)开始事务处理
品牌负载(0.2ms)选择“品牌”。*从“品牌”中选择“品牌”。“id”=?限制?[[“id”,4],“限制”,1]]
变量加载(0.2ms)从“变量”中选择“变量”。*其中“变量”。“id”=?限制?[[“id”,1],“限制”,1]]
品牌存在(0.2ms)从“品牌”中选择1,其中“品牌”。“名称”=?和(“品牌”,“id”!=?)限制?[[“姓名”,“弗雷德里克·马利”],[“身份证”,4],“限额”,1]]
SQL(0.4ms)插入“产品变量”(“变量id”、“产品id”、“数量”)值(?,?)[[“变量id”,1],“产品id”,11],“数量”,20]]
(2.7ms)提交事务
(0.2ms)从“备注零件”的“备注零件”内部连接“产品备注零件”中选择计数(*)。“id”=“产品备注零件”。“备注零件id”中的“产品备注零件”。“产品id”=?[[“产品标识”,11]]
重定向到http://localhost:3000/products/11/edit
15毫秒内完成302次(ActiveRecord:4.0毫秒)

你正在做的
有很多:尽管
。在这种情况下,联接表需要有一个主键(通常为:id)。否则,您无法像此处所需的那样直接访问联接表


另一方面,如果您不需要直接访问联接表(例如,那里没有额外的列),那么您可以设置一个has_和_belish_to_many,而联接表没有id列。

您收到的是
Unpermitted parameter::id
,您发送的
id
属性不在强参数中。谢谢@SebastiánPalma,是的,看到了。我真的不想发送id,因为它是一个联接表,id列不存在。如果我将
id
添加到我的强参数中,我会得到以下错误:
NoMethodError(对于nil:NilClass,未定义的方法
to_sym):`。所以我想我有更大的问题(谢谢,我为表添加了一个主键,现在它可以工作了。
= 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