开发者

Rails: create multiple records of a self-referencing model from a single form

开发者 https://www.devze.com 2023-03-18 20:49 出处:网络
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:

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消