Topic: Most rails-friendly and clean way to represent product bundles?

Hi,

I need to design a DB which should hopefully be relatively straightforward but I'm a bit confused as to how to do it 'the rails way', so to speak.  The site sells 'products'. Here are the relationships and rules that I need to enforce:

1) Each product has a product code, name, description, zero or more images, zero or more PDF files, price, weight, etc.

2) Some products are actually collections of other individual products, but otherwise have the same attributes as a product, such as price, images etc and need to be displayed and behave like any other individual product - think 'bundle of products'.

3) Each product can exist in one or more product categories.

4) Product categories can be any number of levels deep and products can exist at any level.

I have so far thought I would start with a design something like this:

products
images
images_products (many to many table linking products with images)
pdfs
pdfs_products (many to many table linking products with pdfs)
categories (self-referencing table to create category hierarchy)
categories_products (many to many to allow products to exist anywhere in categories)

Assuming this isn't wrong, how do I best achieve rule #2 above?  Do I just need to create a table to join products to itself and how do I model the relationship in rails?

TIA,

Matt.

Re: Most rails-friendly and clean way to represent product bundles?

ukmatt wrote:

products
images
images_products (many to many table linking products with images)
pdfs
pdfs_products (many to many table linking products with pdfs)
categories (self-referencing table to create category hierarchy)
categories_products (many to many to allow products to exist anywhere in categories)

You may not need the images_products and pdfs_products tables. Does a given image/pdf need to relate to many products? It sounds like this is a one-to-many relationship by your description.

ukmatt wrote:

Assuming this isn't wrong, how do I best achieve rule #2 above?  Do I just need to create a table to join products to itself and how do I model the relationship in rails?

See acts_as_tree. You can do the same thing with categories.

Railscasts - Free Ruby on Rails Screencasts

Re: Most rails-friendly and clean way to represent product bundles?

ryanb wrote:

You may not need the images_products and pdfs_products tables. Does a given image/pdf need to relate to many products? It sounds like this is a one-to-many relationship by your description.

Well spotted.  No, I don't. They are many-to-many because I was trying to have enough flexibility to cope with that scenario.  Having thought about it, though, I might as well make it one-to-many and I might see a small performance improvement, too.

I keep forgetting how easy it is to change the structure in rails - my PHP past still sees me building-in stuff 'just in case'!

Thanks very much,

Matt.