Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 772761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:51:14+00:00 2026-05-14T18:51:14+00:00

Sorry about the vague title, but i didnt know how to ask the question

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:51:15+00:00Added an answer on May 14, 2026 at 6:51 pm

    I have to make a few assumptions here, because from your description, your data model is quite unusual. In particular the “Item” class seems to serve no purpose.

    I assume it looks like this:

    class Order < ActiveRecord::Base
       has_many :itemgroups
    end
    
    class Itemgroup < ActiveRecord::Base
       belongs_to :order
       has_many :items
    end
    
    class Item < ActiveRecord::Base
      belongs_to :itemgroup
    end
    

    In which case, the simplest change you can make is to change your loop to this:

     @order.itemgroups.each do |f|
       f.amount.times do
         item = itemgroup.items.build() #this links the fk before it exists
         #this is where I assume you are doing something with the item
         #otherwise the item class seems pointless
      end
     end
     @order.save #saves dependent objects as well
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the vague title but I have a servlet with the following mapping
Sorry about the vague title, but I really don't know how to describe this
Sorry about a vague title, but here's what i am trying to do. I
Sorry about the wording for my question title. I have a basic HTML anchor
Sorry about the vague question title, I just want to ascertain some things. Static
Sorry for vague title, I am not able to describe my question in one
Sorry for the vague title, but not quite sure how to summarize this one.
Sorry for the vague thread title; hard to succinctly describe my question. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.