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.
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:
Then, in some view, you could do something like this:
Edit
See the addition to the Package model.
Here’s how you’d add products to a package: