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 8447113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:06:21+00:00 2026-06-10T10:06:21+00:00

I have a list of items in a table and I’d like to create

  • 0

I have a list of items in a table and I’d like to create collections of those items in a new table. I’ve looked at has_many and has_many :through but I’m not sure those are the right choice and I’m not entirely sure how they’d work in my situation.

One special circumstance is that I want the items from the table to be identified by a unique field called typeID instead of the normal ID.

More information:

My model:

  create_table "products", :force => true do |t|
    t.integer  "typeID"
    t.string   "name"
    t.decimal  "basePrice",   :precision => 17, :scale => 4
    t.datetime "created_at",                                 :null => false
    t.datetime "updated_at",                                 :null => false
  end

I have a bunch of products and I need to have some of them bundled into packages (to sell as a bundle) that I can work with. The products will need to be able to be included in multiple different bundles.

  • 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-06-10T10:06:23+00:00Added an answer on June 10, 2026 at 10:06 am

    I’d recommend a similar approach to Samiron’s if each product only belongs to a single package.

    However, if that’s not the case, I’d recommend has_many :through instead. Here’s an example of that:

    class Package
      has_many :product_listings
      has_many :products, :through => :product_listings
    
      # allows you to make convenient create/build calls like i do below
      accepts_nested_attributes_for :product_listings
    end
    
    class Product
      # defining these relations are only necessary if you want to be able to get all
      # packages a product exists in
      has_many :product_listings
      has_many :packages, :through => :product_listings
    end
    
    class ProductListing
      belongs_to :package
      belongs_to :product
    
      attr_accessible :package_id, :product_id
    end
    

    Then, in some view, you could do something like this:

    Package.all.each do |package|
      <%= package.name %>  # or whatever attribute the package has
      package.products.each do |product|
        <%= product.name %>  # or whatever attribute the product has
      end
    end
    

    Edit

    See the addition to the Package model.

    Here’s how you’d add products to a package:

    package = Package.create(:name => 'some package')
    
    # Rails naming convention for attributes is snake_case, not camelCase (if you care) 
    product = Product.create(:name => 'mouse', :base_price => 20.00)
    
    package.product_listings.create(:product_id => product.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably easy to do but I have a table which list items
I have here a list of items in a table. Each item has a
I have a list on when items have been handed out. The table has
I have created a TableView(Grouped Table View). And I can list the items in
Within my code I have list items like this; <div id=content> <ul class=kwicks> <li
I have a list which is dynamically built, but there are empty list items
I have List with n items. I wish transform my list to new list,
I have a list of items in a MySQL table. The user is able
I have a list of items. Most of these items will not be in
I have a table called transactions with a many-to-many relationship to items through the

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.