[RAILS] How to design that Model?

8 views
Skip to first unread message

Norbert Melzer

unread,
Nov 13, 2011, 1:14:18 PM11/13/11
to rubyonra...@googlegroups.com
Hi!

I have a problem designing the following model. Better I have problems to write the corresponding rails code to get that model up and running as expected.

I tried several combinations of different tables, has_and_belongs_to statements with several options with join_tables and I tried self joins, etc... Really, I dont see a solution...

OK, here the problem:

I have the model "Item" and every "Item" should be craftable. Therefore it needs "Ingredients", this "Ingridients" are "Items" too.

Given Item "Flour"
And Item "Water"
When I mix it
Then I should have the Item "Dough"

But it is not that easy (the above example is complicated enough for me).

Since the Items have all a unit of messurement (Water is litre, Flour is kilograms) I only want to have fractions in the receipe list.

Something like that:
1kg Flour + 0.25 litre Water = 1.25 kg Dough

I really dont know how to do the migration and model for that "Recipe"

Of course there are Items that dont have ingredients (How could I assemble something into water?) or are not needed as Ingredient anymore (What should I build with a skyscraper?)

Could you help me?

TIA
Norbert Melzer

Frederick Cheung

unread,
Nov 14, 2011, 9:17:22 AM11/14/11
to Ruby on Rails: Talk


On Nov 13, 6:14 pm, Norbert Melzer <timmel...@googlemail.com> wrote:

> OK, here the problem:
>
> I have the model "Item" and every "Item" should be craftable. Therefore it
> needs "Ingredients", this "Ingridients" are "Items" too.
>
> Given Item "Flour"
> And Item "Water"
> When I mix it
> Then I should have the Item "Dough"
>
> But it is not that easy (the above example is complicated enough for me).
>
> Since the Items have all a unit of messurement (Water is litre, Flour is
> kilograms) I only want to have fractions in the receipe list.
>
> Something like that:
> 1kg Flour + 0.25 litre Water = 1.25 kg Dough
>
> I really dont know how to do the migration and model for that "Recipe"
>
> Of course there are Items that dont have ingredients (How could I assemble
> something into water?) or are not needed as Ingredient anymore (What should
> I build with a skyscraper?)
>

Just off the top of my head:

an item model, which should have some attributes like what units to
use with it, whether it can be combined with other things, and whether
it is primitive (like your example of water. That might be debatable -
you can make water by mixing hydrogen and oxygen, or you might say
that you get fresh water by desalinating sea water).

Your next model could be an ItemQuantity: an item plus a quantity
(i.e. water, 0.25L) (i.e. item_quantity belongs_to :item), which
reflects a specific use of a certain item in a recipi

Then you could have a step model, where a step is a series of item
quantities, perhaps an action, i.e. uses: water 0.25L, flour 1kg,
action:mix and then a second list of item quantities: the items that
are produced, and in which quantities. You might want action to be a
separate model (perhaps contain explanations of the specific process,
e.g. cream versus fold versus beat etc.)

An Item quantity could have 2 foreign keys pointing a step. The first
would indicate that it is one of the inputs to that step, the second
would mark that it is the output of a step (and obviously an item
quantity might be the output of one step and the input of another,
although never input and output for a single step.

A recipe would then be an ordered list of steps.

You should also think about what you are going to do with these
modelled items - what are the queries you are going to want to run
against your database? If the structure you come up with makes
important queries very difficult or slow then it's worth looking at
whether you can organise your data differently

Fred

Norbert Melzer

unread,
Nov 14, 2011, 12:51:57 PM11/14/11
to rubyonra...@googlegroups.com
2011/11/14 Frederick Cheung <frederic...@gmail.com>:

> an item model, which should have some attributes like what units to
> use with it, whether it can be combined with other things, and whether
> it is primitive (like your example of water. That might be debatable -
> you can make water by mixing hydrogen and oxygen, or you might say
> that you get fresh water by desalinating sea water).

In this particular abstraction of the world water is definitively a
"primitiv" as you call it.

> Your next model could be an ItemQuantity: an item plus a quantity
> (i.e. water, 0.25L) (i.e. item_quantity belongs_to :item), which
> reflects a specific use of a certain item in a recipi
>
> Then you could have a step model, where a step is a series of item
> quantities, perhaps an action, i.e. uses: water 0.25L, flour 1kg,
> action:mix and then a second list of item quantities: the items that
> are produced, and in which quantities. You might want action to be a
> separate model (perhaps contain explanations of the specific process,
> e.g. cream versus fold versus beat etc.)
>
> An Item quantity could have 2 foreign keys pointing a step. The first
> would indicate that it is one of the inputs to that step, the second
> would mark that it is the output of a step (and obviously an item
> quantity might be the output of one step and the input of another,
> although never input and output for a single step.
>
> A recipe would then be an ordered list of steps.

I hope I will have some time this week to think about this and to
understand it all.
Neither english nor ruby are my native languages and im still
learning. Especially all about databases drives me crazy in regular.
It took me about 3 hours this morning to get a "simple" has_many up
and running. Migrating a thousand times or so back and forth.

I never know where to put the belongs_to and where the has_many, or
how to set up the migrations, where to use singular and where plural.
And after hours of trying and googling around, I found somewhere that
I cant use "has_many :attributes" because of some reserved keyword
stuff...

> You should also think about what you are going to do with these
> modelled items - what are the queries you are going to want to run
> against your database? If the structure you come up with makes
> important queries very difficult or slow then it's worth looking at
> whether you can organise your data differently

Every day use will be two types of query, only when a user requests it
for a particular Item:

1. What is the recipe for this item?
2. In which recipes is this item used?

And as I am writing this I am thinking about to allow production of
fractional items for some cases. But I will wind a solution for this
myself.

A very seldom and only administrativ query would be to iterate over
all items and build up a production graph.

So far I thank you for your suggestions, I will post again if I have
any problems.

Norbert

Colin Law

unread,
Nov 14, 2011, 3:14:35 PM11/14/11
to rubyonra...@googlegroups.com
On 14 November 2011 17:51, Norbert Melzer <timm...@googlemail.com> wrote:
>
> I hope I will have some time this week to think about this and to
> understand it all.
> Neither english nor ruby are my native languages and im still
> learning. Especially all about databases drives me crazy in regular.
> It took me about 3 hours this morning to get a "simple" has_many up
> and running. Migrating a thousand times or so back and forth.
>
> I never know where to put the belongs_to and where the has_many, or
> how to set up the migrations, where to use singular and where plural.

I suggest that you work through some of the Rails Guides, start with
Getting Started and Active Record Associations. Also the one on
debugging will come in handy. Then work right through a good tutorial
such as railstutorial.org, which is free to use online. Then you
should have a better grasp of the fundamentals of Rails.

Colin

tom

unread,
Nov 14, 2011, 5:51:48 PM11/14/11
to rubyonra...@googlegroups.com
it seems that u have 2 goals in mind
a) recipes
b) ingredients

since a dish consists of ingredients b and b itself of many b u should look into simple treebased appraoch:

ingredient:
-id
-parent_id
-name
-uom (=unit of measure)
-qty (=quantity)







--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Norbert Melzer

unread,
Nov 15, 2011, 3:05:09 PM11/15/11
to rubyonra...@googlegroups.com
2011/11/14 tom <toma...@gmail.com>:

> ingredient:
> -id
> -parent_id
> -name
> -uom (=unit of measure)
> -qty (=quantity)

With this approach I could use water and flour only to produce Dough.

But I want to use the water in another recipe to brew a beer and there
fore I need other qantities. So I think the approach of Frederick
seems to fit my needs more

tom

unread,
Nov 15, 2011, 5:01:47 PM11/15/11
to rubyonra...@googlegroups.com
no.
imagine u go from the top, and the deeper u traverse into the tree, the more details u get. its secondary if the tree reflects ingredients, recipes, or a dish.
as soon something is made up of something else, your best bet is a tree.
but, as usual, doing by learning 
good luck


Reply all
Reply to author
Forward
0 new messages