Ruby on rails 3.2 如何添加和编辑多个具有嵌套表单和多个:到
我相信我已经阅读了所有相关的答案,但仍然没有找到解决方案,所以我的问题是。我“继承”了一个应用程序,它可以处理产品、批次和数量。一个批次将有许多产品,产品可以出现在许多批次和数量。因此a has_many:through关系是隐含的,因为联接表将有一个额外的列用于产品数量Ruby on rails 3.2 如何添加和编辑多个具有嵌套表单和多个:到,ruby-on-rails-3.2,nested-forms,has-many-through,Ruby On Rails 3.2,Nested Forms,Has Many Through,我相信我已经阅读了所有相关的答案,但仍然没有找到解决方案,所以我的问题是。我“继承”了一个应用程序,它可以处理产品、批次和数量。一个批次将有许多产品,产品可以出现在许多批次和数量。因此a has_many:through关系是隐含的,因为联接表将有一个额外的列用于产品数量 class Batch < ActiveRecord::Base has_many :quantities has_many :products, :through => :quantities acc
class Batch < ActiveRecord::Base
has_many :quantities
has_many :products, :through => :quantities
accepts_nested_attributes_for :products
end
class Product < ActiveRecord::Base
has_many :quantities
has_many :batches, :through => :quantities
accepts_nested_attributes_for :quantities
end
class Quantity < ActiveRecord::Base
belongs_to :product
belongs_to :batch
end
但是我仍然在表单上苦苦挣扎——应该如何指定字段
<%= form_for(@batch) do |f| %>
<% f.fields_for :products do |prod_form| %>
<% prod_form.fields_for :quantity do |qty_form| %>
<% end %>
<% end %>
<% end %>
答案与找到的答案相同
对于has_1和has_多个关联,生成方法签名是不同的
class User < ActiveRecord::Base
has_one :profile
has_many :messages
end
的生成语法有一个关联:
user.messages.build
user.build_profile # this will work
user.profile.build # this will throw error
有关更多详细信息,请阅读has___-one关联文档。此处的答案:答案与找到的答案相同
user.build_profile # this will work
user.profile.build # this will throw error