0
我有一个产品表和相关产品的表,相关工作的一种方式:填充自连接表中的Rails 4
Product(id: int, name: string)
RelatedProduct(id: int, product1: int, product2: int)
有如下型号:
class Product < ActiveRecord::Base
validates :name, :uniqueness => true, :presence => true
has_many :relations, :class_name => "RelatedProducts", :foreign_key => :product1, inverse_of: :source
has_many :related_products, through: :relations, :source => :destination
end
class RelatedProducts < ActiveRecord::Base
belongs_to :source, :class_name => "Product", inverse_of: :relations
belongs_to :destination, :foreign_key => :product2, :class_name => "Product"
end
这工作,如果我预填充RelatedProducts表,show view将正确显示所有相关产品。我无法弄清楚如何从html中填充RelatedProducts?
<%= form_for(@product) do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="related-products field">
<h3>Related Products</h3>
<%= f.collection_check_boxes :related_products, Product.where.not(id: @product.id), :self, :name %>
</div>
<div class="actions">
<%= f.submit 'Update Products' %>
</div>
<% end %>
强参数被定义为:
params.require(:product).permit(:name, related_products: [:id, :name])
也许尝试为related_products模型制作form_for – amb110395