Ruby 尝试将嵌套表单与Rails 4中的has_many:through一起使用,并获取重复的联接模型条目 客观的
使用has\u many:through和嵌套属性保存带有产品清单的合同 问题 合同保存后,产品将正确创建,舱单将创建两次,一次使用数量,但不使用关联,另一次使用关联,但不使用数量 细节 系统信息 操作系统:10.9.3 Ruby版本:Ruby 2.0.0p353 2013-11-22修订版43784[x86_64-darwin13.0.0] Rails版本:Rails 4.0.2 模型 合同 参数和插入 如您所见,清单记录被创建了两次。另一个问题是这两个清单记录如下所示:Ruby 尝试将嵌套表单与Rails 4中的has_many:through一起使用,并获取重复的联接模型条目 客观的,ruby,ruby-on-rails-4,nested-attributes,has-many-through,Ruby,Ruby On Rails 4,Nested Attributes,Has Many Through,使用has\u many:through和嵌套属性保存带有产品清单的合同 问题 合同保存后,产品将正确创建,舱单将创建两次,一次使用数量,但不使用关联,另一次使用关联,但不使用数量 细节 系统信息 操作系统:10.9.3 Ruby版本:Ruby 2.0.0p353 2013-11-22修订版43784[x86_64-darwin13.0.0] Rails版本:Rails 4.0.2 模型 合同 参数和插入 如您所见,清单记录被创建了两次。另一个问题是这两个清单记录如下所示: Manifest.l
Manifest.last(2)
Manifest Load (0.3ms) SELECT "manifests".* FROM "manifests" ORDER BY "manifests"."id" DESC LIMIT 2
=> [#<Manifest id: 5, contract_id: nil, product_id: 4, quantity: 3, created_at: "2014-06-06 16:39:43", updated_at: "2014-06-06 16:39:43">, #<Manifest id: 6, contract_id: 6, product_id: 4, quantity: nil, created_at: "2014-06-06 16:39:43", updated_at: "2014-06-06 16:39:43">]
第一个是使用数量集创建的,但未保存关联。第二个是通过适当的关联创建的,但不是数量
我错过了什么?我敢肯定这是件小事或愚蠢的事,但我已经有一段时间把头撞在这堵墙上了,所以我希望神奇而聪明的堆栈溢出社区能帮我解决这个问题。谢谢你能提供的任何帮助 可能不起作用
试试这个:
class Contract < ActiveRecord::Base
has_many :manifests, dependent: :destroy, inverse_of: contract
has_many :products, through: :manifests
accepts_nested_attributes_for :products
end
然后:
= f.fields_for :manifests do |manifest_builder|
= manifest_builder.fields_for :products do |product_builder|
# etc.
然后,所有内容都应该正确保存,因为外键可以滴入。这在逻辑上似乎更有意义
否则,您可能可以修改创建操作,如:
@contract = Contract.new(contract_params)
@contract.products.first.manifests.first.contract = @contract
但这似乎有点粗糙。忽略表单中的间距。。。我在这篇文章中没有正确缩进。我在一些地方看到过这一点,但不确定我的用例是否匹配。我来试试看会发生什么。谢谢不。没有变化不过,我会对这个选项做更多的阅读,看看我是否能找到答案。@ZacharyAbresch,我用其他几种可能的解决方案更新了我的答案。
= form_for @contract do |f|
- if @contract.errors.any?
#error_explanation
%h2= "#{pluralize(@contract.errors.count, "error")} prohibited this contract from being saved:"
%ul
- @contract.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.field
= f.label :description
%br/
= f.text_area :description
%fieldset
= f.fields_for :products do |product_builder|
.field
= product_builder.label :sku
= product_builder.text_field :sku
.field
= product_builder.label :name
= product_builder.text_field :name
.field
= product_builder.label :description
%br/
= product_builder.text_area :description
.field
= product_builder.label :unit_price
= product_builder.text_field :unit_price
= product_builder.fields_for :manifests do |manifest_builder|
.field
= manifest_builder.label :quantity
= manifest_builder.number_field :quantity
.actions
= f.submit 'Save'
Started POST "/contracts" for 127.0.0.1 at 2014-06-06 11:39:43 -0500
Processing by ContractsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"W1FTKbimANj6SzkpPsyVV1BD7RU9PHVvFYWOdSdXmJU=", "contract"=>{"name"=>"Testing Contract", "description"=>"A test", "products_attributes"=>{"0"=>{"sku"=>"mas-001", "name"=>"Product Test", "description"=>"Products description", "unit_price"=>"100.50", "manifests_attributes"=>{"0"=>{"quantity"=>"3"}}}}}, "commit"=>"Save"}
(0.2ms) begin transaction
SQL (0.7ms) INSERT INTO "contracts" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00], ["description", "A test"], ["name", "Testing Contract"], ["updated_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00]]
SQL (0.6ms) INSERT INTO "products" ("created_at", "description", "name", "sku", "unit_price", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00], ["description", "Products description"], ["name", "Product Test"], ["sku", "mas-001"], ["unit_price", 100.5], ["updated_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00]]
SQL (2.9ms) INSERT INTO "manifests" ("created_at", "product_id", "quantity", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00], ["product_id", 4], ["quantity", 3], ["updated_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00]]
SQL (0.3ms) INSERT INTO "manifests" ("contract_id", "created_at", "product_id", "updated_at") VALUES (?, ?, ?, ?) [["contract_id", 6], ["created_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00], ["product_id", 4], ["updated_at", Fri, 06 Jun 2014 16:39:43 UTC +00:00]]
(4.7ms) commit transaction
Manifest.last(2)
Manifest Load (0.3ms) SELECT "manifests".* FROM "manifests" ORDER BY "manifests"."id" DESC LIMIT 2
=> [#<Manifest id: 5, contract_id: nil, product_id: 4, quantity: 3, created_at: "2014-06-06 16:39:43", updated_at: "2014-06-06 16:39:43">, #<Manifest id: 6, contract_id: 6, product_id: 4, quantity: nil, created_at: "2014-06-06 16:39:43", updated_at: "2014-06-06 16:39:43">]
class Contract < ActiveRecord::Base
has_many :manifests, dependent: :destroy, inverse_of: contract
has_many :products, through: :manifests
accepts_nested_attributes_for :products
end
def new
@contract = Contract.new
manifest = @contract.manifests.build
product = manifest.products.build
end
= f.fields_for :manifests do |manifest_builder|
= manifest_builder.fields_for :products do |product_builder|
# etc.
@contract = Contract.new(contract_params)
@contract.products.first.manifests.first.contract = @contract