Topic: Modelling a model on another model

Hi all,

Im building a shopping cart style of thing at the moment for kicks, now the items once selected and paid for will go through several stages.  'On hold', 'Out of Stock', 'Ready to ship' that kind of thing, so ive been using validates_inclusion_of on the items model like so..

validates_inclusion_of :status, :in => ['On hold', 'Out of Stock', 'Ready to ship']

I then had a bit oif an idea, what if I make the status a model itself then I make one belong to the other.  So I did, that in itself is working, but is there any way I can validate items model so that the status has to exist in the status model, so something like...

validates_inclusion_of :status_id, :in => ItemsStatus.find_all

Can that be done?

Re: Modelling a model on another model

Yes it can be done, but maybe its a bit overkill to have a whole model just to represent status. It might be better to use an array or hash. You could then store the array index in the database.

STATUS = ['On hold', 'Out of Stock', 'Ready to ship']

STATUS[0] => 'On hold'

Its a suggestion, there are other ways of doind it.

Re: Modelling a model on another model

I was going to store it in a Model for expansion reasons, say we need a new status, I don't have to edit the code, just add another status into the DB.  I want to use this model for something else you see which is going to have several 'attributes' and is likely to grow so Im trying to implement it on this for practice smile