[LRUG] Matching database tables through Ruby
Murray Steele
murray.steele at gmail.com
Tue Aug 14 09:05:03 PDT 2007
On 14/08/07, Marcus Roberts <marcus at marcusr.org.uk> wrote:
>
> 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?
You could use some const_missing trickery on the model, such that
Status::OPEN would result in returning Status.find_by_name('Open'). I've
certainly seen that in the wild, and we use it in one of our apps here.
class Status < ActiveRecord::Base
def self.const_missing(name)
if value = self.find_by_name(name.to_s)
self.const_set(name, value)
value
else
super
end
end
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20070814/1f5438b8/attachment-0003.html>
More information about the Chat
mailing list