[LRUG] Matching database tables through Ruby

Marcus Roberts marcus at marcusr.org.uk
Tue Aug 14 08:52:26 PDT 2007


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









More information about the Chat mailing list