views:

71

answers:

2

Hi

I'm trying to build an elixir model in which I have a class with a list(of variable size) of tuples.

One example would be a recipe

while I can do something like this:

class Recipe(Entity):
    ingrediants = OneToMany('IngrediantList')
    cooking_time = Field(Integer)
    ...

class IngrediantList(Entity):
    ingrediant = ManyToOne('Ingrediant')
    quantity = Field(Number(3,2))

class Ingrediant(Entity):
    name = Field(String(30))
    ...

It has a number of short comings. For one, I don't like creating an entity for ingrediant list which I don't have any meaning for wrt the domain; takes fun out of abstraction.

Another is that a query like which items can I prepare with this ingredient would get really messy and probably inefficient without adding more relations and or fields to the model making it messy in turn.

One more example would be a bank deposit slip with list of denominations and quantity.

What is the best way to design such a model?

A: 

This is the correct way to model composite objects. The only thing I'd change is the name of the IngredientList class. Something like RecipeEntry or IngredientQuantity would be more appropriate. Calling it a tuple is just trying to avoid the need to name the fact that a recipe needs some quantity of some ingredient.

If for most of the code you don't want to consider the details of the association you can use sqlalchemy associationproxy extension to create a proxy attribute to hide the details.

Ants Aasma
A: 

solution using sqlalchemy associationproxy This is from the sqlalchemy docs.

TMaYaD