views:

44

answers:

1

I'm new to rails and need some help with DB design.

It is basically category, product, quantity, price...type of tables.

I have 4 categories: CatA CatB CatC CatD

Each category above will have multiple products in them:

CatA - ProductA1, ProductA2, ProductA3
CatB - ProductB1, ProductB2, ProductB3
CatC - ProductC1, ProductC2, ProductC3
CatD - ProductD1, ProductD2, ProductD3

Each Product then has a type, rating, quantity, price, volume

Would it be good to have one big Product table with columns: Category, Product Name, Type, Rating, etc..ec..

or have different tables for Category, Product, Type which relationship will they have belongs_to or has_many?

I'm looking at it from Admin Point of View. If a user wants to add a new product then they'll first select category from drop down, type product name, select product type etc. having everything in one table will be easy but is that efficient?

+2  A: 

You're going to want products and categories tables.

Product will belong_to :category and Category will has_many :products.

You'll need a category_id column in the products table to make the association.

I wouldn't recommend a types table -- just have the type as a column in the products table. Don't use the word "type" though, go with "kind" as Rails uses "type" for Single Table Inheritance so you might run into some weirdness if you're not using it for that.

bensie
Why de-normalize "types"? How is that different from Category? Stick with good DB practice, don't duplicate data. If a Type is reusable by more than one Product, definitely have a types table.
Dave Sims
Yeah, "type" is just another kind of category.
Eric Hill
Based on your info above, it looked like type was more of an attribute than a relation. If you intend to query based on the type (as opposed to just displaying it as part of a product), then I would agree that another table is the way to go.
bensie