Вопрос

I've been following the HABTM Railscast and am looking to extend it.

The Railscast way is great for adding categories to a product. But lets say I have (say) 5 ways of categorising products along different dimensions (and these change occasionally).

With the solution provided I would need to add 2 more models to my app each time I add a new 'dimension'.

Essentialy I want to move from a model like:

Product: Settlers of Catan
Categories: Board Games, Toys etc

to:

Product: Settlers of Catan
Category: Board Games, Toy etc
Age Range, 1-10, 11-20
Cost: High

The Railscast solution is here: http://railscasts.com/episodes/17-habtm-checkboxes-revised?view=comments

The existing solution, to be extended:

Product.rb

has many categorisations
has many categories, through categorisations

Categorisation.rb

belongs to many products
belongs to many categories

Category.rb

has many products, through categorisation
has many categorisations

I could do something like:

Product.rb

has many AgeRangeCategorisations
has many AgeRanges, through AgeRangeCategorisations

AgeRangeCategorisation.rb

belongs to many products
belongs to many AgeRanges

AgeRange.rb

has many products, through AgeRangeCategorisation
has many AgeRangeCategorisations

and

Product.rb

has many CostCategorisations
has many Costs, through CostCategorisations

CostCategorisation.rb

belongs to many products
belongs to many costs

Cost.rb

has many products, through CostCategorisation
has many CostCategorisations

But, it doesn't feel like the Rails way to create new models for each 'category type'.

I am think something along the lines of:

Product.rb

has many CategoryTypes
has many CategoryTypes, through CategoryTypeCategorisations

CategoryTypeCategorisations.rb

belongs to many Products
belongs to many CategoryTypes

CategoryType.rb

belongs to many CategoryTypeCategorisations
belongs to many categorisations
has many categories, through categorisations

Categorisation.rb

has many products
has many categories

Category.rb

has many products, through categorisation
has many categorisations

A CategoryType being the dimension (Age, Cost etc) and the category being the valid list of answers for the CategoryType.

This is getting a little confusing now with so many 'belongs to's and 'has many's floating about

Is this the correct way to try to approach this issue? How else could I approach this?

Это было полезно?

Решение

Your last variant is one way to approach this. For sure you do not want a class for every category type. Unless maybe those category types come with a lot of extra business logic that defines how a product 'behaves', in which case something like Single Table Inheritance would be the better way to model this. But as you describe it I guess those are more like tags, mostly some additional description. (If there are only two or three category types and it stays that way, the number of classes won't matter that much, but if you plan more I would avoid this)

There would be one alternate option I want to mention at least for completeness and because it would save you one table and be less complex that way. This would be to model these categories simply as a hierarchy where your category types are parents and the categories are children.

Category
  - Board Games
  - Toy
Age Range
  - 1-10
  - 11-20
Cost
  - Low
  - Medium 
  - High

This is more like a 'classical' approach to this problem.

Also (especially if you have a lot of products and categories) maybe using some faceted search engine (Solr, Elastic, Sphinx...) or NOSql storage with similar abilities could make searching by several properties very easy.

Другие советы

Use the postgres HStore data type as a column on the products model, and just add these things as key: value pairs.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top