Topic: Am I designing this right? Polymorphism, acts_as_tree, & STI, Oh my
As I write this I'm not sure there's any Polymorphism going on here,
But here are the models, nonetheless:
1) FOOD acts_as_tree, with STI the type which may be "ingredient" or no type
* FOOD: id, name, parent_id, type
* INGREDIENT < FOOD (Ingredients may relate to other foods or other ingredients)
2) Foods have RELATIONSHIPS to other foods (HABTM back to the food table with an extra
field of RELATIONSHIP (this could be a string or I could use TYPE and do STI with
the RELATIONSHIP model as well). RELATIONSHIPS type example would be "contains"
* RELATIONSHIP: primary_food_id, secondar_food_id, (TYPE | RELATION)
* RELATION = "contains" OR ContainsRelation < Relationship OR perhaps...
IngredientRelation < Relationship (SHARE the type / STI between the two classes/tables)
3) Finally, I'm thinking to have some sort of generic TREE class, which pulls the dealings
of the tree out of the FOOD model.
* The FOOD Class will _build_ a TREE.
* Then the Tree class will deal with things like
* Tree.to_s => "Dairy/Cheese/Gouda" and
* Tree.to_dir =>
There are a couple of things I'm worried about in the design:
1) I've had problem with Polymorphism and STI cohabitating together in the past and ended up dealing with "type" thing on my own
2) Performance: Specifically, the tree... I'm worried about how many querries it takes to find Gouda's full path ("Dairy/Cheese/Gouda"). But also, the combination of STI, acts_as_tree, and the relationship table...
Any thoughts on my design are welcomed.
Thank you & Happy Holidays