Creating objects and referencing before saving object to db

Posted by Flexo on Stack Overflow See other posts from Stack Overflow or by Flexo
Published on 2010-05-07T11:23:48Z Indexed on 2010/05/07 11:28 UTC
Read the original article Hit count: 168

Filed under:
|
|
|

Sorry about the vague title, but i didnt know how to ask the question in one line :)

I have an order with nested itemgroups that again have nested items. the user specify the amount of item that he would like to have in each itemgroup. I would like to create these items in the create method of the orders controller when the order itself is being created.

I kinda have 2 problems here. First, how do i set the reference of the items, or better yet, put the items into the @order object so they are saved when the @order is saved? the items are being stored in the db as the code is now, but the reference is not set because the order is not stored in the db yet so it doesnt have an id yet.

Second, im not sure im using the correct way to get the id from my itemgroup.

@order = Order.new(params[:order])

@order.itemgroups.each do |f|
  f.amount.times do
    @item = Item.new()
    @item.itemgroup_id = f.id
    @item.save
    end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rails