Ruby on rails 嵌套表单和数据库更新

Ruby on rails 嵌套表单和数据库更新,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我对rails还很陌生,所以我的问题可能看起来很愚蠢 我有自我联想。一个产品可以成为产品的“组合”,有N个产品 class Product < ActiveRecord::Base has_many :combo_elements, class_name: "ComboElement", foreign_key: :combo_id has_many :element_combos, class_name: "ComboElement", foreign_key: :element

我对rails还很陌生,所以我的问题可能看起来很愚蠢

我有自我联想。一个产品可以成为产品的“组合”,有N个产品

class Product < ActiveRecord::Base
  has_many :combo_elements, class_name: "ComboElement", foreign_key: :combo_id
  has_many :element_combos, class_name: "ComboElement", foreign_key: :element_id
  has_many :combos, :through => :element_combos
  has_many :elements, :through => :combo_elements

  accepts_nested_attributes_for :elements
end

class ComboElement < ActiveRecord::Base
  belongs_to :combo, :class_name => 'Product'
  belongs_to :element, :class_name => 'Product'
end
谢谢你的帮助。 提前谢谢

<%= f.simple_fields_for :elements, @element do |b| %>
  <%= b.input :element_id, :collection => Product.all.collect{ |t| [t.name, t.id ]}, selected: b.object.id %>
  <%= b.link_to_remove "Remove this element" %>
<% end %>
def product_params
  params[:product].permit(:name, :price, elements_attributes: [:id, :element_id, :_destroy])
end