I have 2 mysql tables, Product and Color:
Color
ID ColorName
1 Red
2 Green
3 Yellow
4 Blue
Products
ID Color1 Color2 Color3 ProductName
1 ? ? ? Orco
2 ? ? ? Skeletor
3 ? ? ? He-Man
I need to display the ColorName to web visitors when they view
Products.
I can do a number of things:
-
put Color.ID into Product.Color1, Product.Color2 and Product.Color3.
These would be Foreign Keys and from what I read, RoR likes that. -
Create a new table called ProductColors which would have an ID,
ColorID, and ProductID columns as used in many-to-many relations. -
Use ColorName varchar in the Products table for Color1, Color2, and
Color3.
My actual database has a number of tables similar to Color, where all I
really need to do is link a varchar field to the Products table.
- Which solution would be more efficient? Using ID’s seems like too
big a hit when viewing a product. - Can RoR support using a string/varchar as a foriegn key? Ie. Take
out the Color ID and make the ColorName a Primary Key and link it to
the Products table?
Please help
Jared
Let’s say a table called Color has an int column ‘ID’ and a varchar
column ‘ColorName’ and a table called Products lists Products. Each
product has 4 colors. All I need to do is display the 4 colors to
visitors to the website. Why not just put the strings ‘Red’ ‘Green’
etc… into the Product as opposed to using the Color ID’s or even a
whole new table