[LRUG] Matching database tables through Ruby

Martin Sadler mtsbtt at googlemail.com
Tue Aug 14 09:03:20 PDT 2007


Hi Marcus,

I tend to use the acts_as_enumerated plugin:
http://agilewebdevelopment.com/plugins/acts_as_enumerated

Example:
task = Task.new(:status => TaskStatus[:open])
task.update_attribute(:status, TaskStatus[:closed])

Very handy.

More info in the ReadMe here:
http://svn.protocool.com/public/plugins/enumerations_mixin/ 
README_ENUMERATIONS


Hope that helps,
Martin.

On 14 Aug 2007, at 16:52, Marcus Roberts wrote:

> I'm working on a piece of code at the moment that has raised a
> problem I've seen a couple of times.
> This is for a Rails app, but comes up generally.
>
> I have a tasks table, where a task has a status field, indicating if
> it's "Open", "In progress" or "Closed"
>
> I also have a database table called status, which to keep it simple
> looks like:
>
> 1, "Open"
> 2, "In Progress"
> 3, "Closed"
>
> I use a database table so the status types perhaps can be added to in
> the future through the web interface.
>
> A simplistic approach to setting a task to "In Progress" is to say
>
> Task.status = 2
>
> or to check if a task is closed:
>
> if task.status==3 ...
>
> Using hard coded values like that is an obvious bad thing to do.
>
> Is there a nice pattern for making the link between values in a
> database and the Ruby code?
>
> If I didn't want to change the status types available in the future,
> what's the best way to hard code them into the Ruby itself, assuming
> this use of a database table is the wrong way to do it?
>
> Thanks
>
> Marcus
>
>
>
>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org




More information about the Chat mailing list