开发者

Creating a record and adding associated records to it

开发者 https://www.devze.com 2022-12-18 07:17 出处:网络
I have a following model class Order < ActiveRecord::Base has_many :products, :through => :line_items

I have a following model

class Order < ActiveRecord::Base
    has_many :products, :through => :line_items
end
class Product < ActiveRecord::Base
    belongs_to :order
end

line_items is a table, that associates an Order with multiple products.

create_table "line_items", :force => true do |t|
    t.integer  "order_id"
    t.integer  "product_id"
    t.integer  "count"
    t.datetime "created_at"
    t.datetime "updated_at"
end

So, each order can have mult开发者_如何学运维ilple products.

I need to create a form, that allows user to create an order and to include some products to it. For each product, the quantity can be setted. I think, the classic solution of this problem, via keeping a cart (or basket) in session, not matches my problem, because i need to setup and send all the stuff once, without clicking on each product's buy button and waiting.

Are there any best practices to implement this ?


Check out these two:

  • http://railscasts.com/episodes/73-complex-forms-part-1
  • http://railscasts.com/episodes/196-nested-model-form-part-1
0

精彩评论

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

关注公众号