开发者

Ruby on Rails 3 - Modeling Products with Many Categories / Attributes

I am working on my first Rails website, a sort of shopping website: there are products sold through the website, each with multiple unique attributes.

The use case I am imagining is: a user visits the site looking to buy a used bed, they click through several higher categories until they find a bed of the correct size, something like 开发者_JS百科this:

Furniture
---> Couch
---> Dresser
---> Bed
-------> Size
----------> King
----------> Queen
...

At each time the user clicks a more narrow category, they are supplied a menu with the next level of detail (a la NewEgg). When "Furniture" is clicked, all furniture is displayed, and a menu with the types of furniture appears. When "Bed" is clicked, all beds are displayed, and a menu showing the various attributes of a bed appears, etc.

So far, I have an "Item" parent class which contains all the attributes of every item sold through the website (price, description, etc...). I am stuck with what to do next.

My first instinct is to have subclasses, but they don't seem to be that useful. There is nothing about the "Furniture" subclass that would have anything new in it, other than definiting it to be furniture. Also, each parent class needs to know about its subclasses.

What sort of design pattern should I pursue to cleanly implement this model? I've been looking into nested sets, but I am not completely sure that's the way to go. Any guidance is appreciated.

Thanks!


Your categories clearly form a tree, which is probably why you're thinking about a class inheritance tree. However, you are correct that this isn't the right model, since subclasses do not add any functionality over their parents.

You should have a single "Item" class, which is a node in the category tree - with references to its parent category (another "Item", or null for the root) and its array of children (all "Item"s).

The correct place to store the information about the tree structure is the database. In your items table, add a "parent_id" column as a foreign key - to the items table.

As the user navigates down in the tree you show the subcategories by querying for the items whose parent_id equals the current item id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜