Rails: create multiple records of a self-referencing model from a single form
I have a self-referencing sales model. A sale object can represent an "independent" product sale, sale of a product set or a sale of a product within a set:
create_table :sales do |t|
t.belongs_to :product
t.belongs_to :set
t.integer :set_sale_id
...
end
set_sale_id references a parent record of a set sal开发者_开发知识库e:
class Sale < ActiveRecord::Base
belongs_to :product
belongs_to :set
# parent sale (set sale)
belongs_to :set_sale, :class_name => 'Sale'
# nested product sales
has_many :product_sales, :class_name => 'Sale', :foreign_key => :set_sale_id
end
I want to submit several product and set sales from a single form like this one:
------------------- |Product 1 \/| ------------------- ------------------- |Product 2 \/| ------------------- -------------- |Set 1 \/| -------------- -------------- |Product 2 \/| -------------- |Product 3 \/| -------------- ------------------- |Select product \/| ------------------- ---------- | Submit | ----------
The question is how to do it an optimal way using <%= f.fields_for ... %> for nested records?
How to handle submitted records on the controller side?
精彩评论