# How best to implement lookup table?

**URL:** <https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135>\
**Category:** Rails\
**Created:** [May 10, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135 "2006-05-10T17:05:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/1 "2006-05-10T17:05:45Z")

</div>

Hi all -

I’d like to implement a lookup table in my app that contains some  
reference data, just some status codes and their descriptions. What’s  
the most appropriate approach according to the “rails way” of doing  
things? Would I just implement a has\_many relationship? Let’s say  
these are Order status codes, would the StatusCode have many Orders?  
This seems right from a database POV (normalizing the code and  
description into a separate table), but sort of goofy from a coding  
POV. “Order belongs\_to :order\_status\_code” seems a little off to me.  
It would be nice if I could somehow automatically have the status  
description automatically be added as a read-only attribute to the Order  
class.

Suppose the above approach is the correct one. Would I still use the  
standard id column as my primary key, or just use the status code as I  
normally would in a non-rails app? It seems like it would make sense to  
do the latter, but would I regret going against rails conventions when  
it is just as easy to go along with it?

Thanks for any insight into this. I’m having a blast learning Rails.

Ken

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/2 "2006-05-10T19:44:21Z")

</div>

Just put a hash in your Order model, together with an accessor function  
if you want one:

class Order \< ActiveRecord::Base  
belongs\_to :customer  
has\_many :line\_item

ORDER\_STATUS = {  
‘OP’ =\> ‘Open’,  
‘SH’ =\> ‘Shipped’,  
‘CN’ =\> ‘Cancelled’  
}.freeze # freeze to make this hash constant

def status\_description {  
return ORDER\_STATUS[self.status]  
}  
end

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/3 "2006-05-10T19:59:10Z")

</div>

On 5/10/06, Kenneth L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It would be nice if I could somehow automatically have the status

Mick’s suggestion is good. Another way is to turn the status code  
into a regular Ruby class that you can add additional methods and data  
to, while still keeping it as a single column in your db.

Assuming your table has a status\_code column…

class Status  
def initialize(code)  
@code = code  
end

def code  
# something to return a useful status from the numeric value in the  
DB  
end  
end

class Order \< ActiveRecord::Base  
composed\_of :status, :mapping =\> %w(status\_code status)  
end

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/4 "2006-05-10T20:23:36Z")

</div>

I personally like to use separate tables for lookup tables, rather than  
hash or class oriented approaches. The big reason: you can edit the  
values in the lookup table down the road, even if its only from  
something as simple as a generated scaffold. I think the code is a  
terrible place to define what possible values there should be for  
something like status.

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/5 "2006-05-10T20:57:56Z")

</div>

Thanks everyone for your replies! They’re all very well thought out.

I think I’ll go with the table and has\_many approach, simply because it  
will be easier for my client to edit the descriptions.

Another drawback with the pure-ruby approaches is that not storing the  
descriptions in the database will make it difficult for other  
applications (say a reporting app) to use the data, as they won’t have  
the descriptions available. (unless you want to get all philosophical  
and say that other applications shouldn’t access the data directly and  
you should use web services)

Now the question is, is it ok to use the code as the primary key in the  
lookup table? Are there any gotchas to using something besides the  
automagical “id” column?

Ken

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/6 "2006-05-10T20:26:51Z")

</div>

I think these methods work well unless you want the lookup values to be  
easy  
for someone to edit (e.g. admin). In that case, the best solution I  
have  
come up with is to create a table for each lookup and use the has\_many  
relationship. I saw a writeup somewhere where someone used a single  
lookup  
table for all lookups in their application, but that requires a column  
to  
distinguish which lookup each entry belongs to and seems little messy to  
me. Of course having a dozen lookup tables also feels messy at times.

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 10, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/7 "2006-05-10T20:33:11Z")

</div>

On 5/10/06, Bryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I personally like to use separate tables for lookup tables, rather than  
> hash or class oriented approaches. The big reason: you can edit the  
> values in the lookup table down the road, even if its only from  
> something as simple as a generated scaffold. I think the code is a  
> terrible place to define what possible values there should be for  
> something like status.

Agreed, definitely, when you need to edit the codes frequently, or  
without restarting the app.

I feel that full-on tables with models are overkill for simple status  
columns, though.  
For example, what if you only have:  
0 =\> AWAITING\_REVIEW  
1 =\> ACTIVE  
2 =\> SOMETHING\_ELSE  
3 =\> CLOSED

…or something similar. Four rows maybe wouldn’t be worth a whole  
table, but you need a numeric code for faster indexing, etc, etc.

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 11, 2006, 12:20am UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/8 "2006-05-11T00:20:44Z")

</div>

Why bother trying to use a different key format? It’ll “just work” with  
autonumbering ids, and you can still put an index on that field for  
faster lookups if you need it. If you’re worried about having to do two  
queries to get an item and its status, use the preloading feature that  
AR provides (:include =\> …).

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 11, 2006, 1:28am UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/9 "2006-05-11T01:28:27Z")

</div>

Hey,

I wrote a plugin for this very thing. Rather than has\_many and  
belongs\_to you have has\_enumerated and acts\_as\_enumerated.

It gives you extra goodness like caching and being able to write code  
that looks (imho) better:

class Foo \< ActiveRecord::Base  
has\_enumerated :status  
end

class Status \< ActiveRecord::Base  
acts\_as\_enumerated, :order =\> ‘position ASC’  
end

foo = Foo.new()  
foo.status = :open  
foo.status = Status[:open]

You can get details for the plugin at http://  
[www.agilewebdevelopment.com/plugins/show/4](http://www.agilewebdevelopment.com/plugins/show/4)

It allows separate tables for each enumeration or you can put them  
all in the same table (it uses STI in that case).

It also has ‘virtual’ enumeration classes - so if you have 20  
different enumeration types you don’t need to have 20 different files  
in your models directory.

The only thing which might make it unsuitable for you is that it  
caches the values in-memory only - and I make it purposefully hard to  
change the record values as a result (don’t want 3 different fcgi  
listeners getting out of sync). So, if you plan on frequently  
changing the values for Status records then you should look at  
another solution.

Personally, I only change these sorts of records (like status codes)  
during a deploy anyhow.

Hope this helps,  
Trevor

–  
Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 11, 2006, 2:23am UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/10 "2006-05-11T02:23:57Z")

</div>

On 10-May-06, at 4:49 PM, Eden B. wrote:

> Do you have any views on this idea?

There’s plenty of ways to do it, ranging from locking the whole user-  
side of the app with a “down for maintenance” message, to versioning  
your data so that when you’re happy with your changes you make the  
new version ‘live’ (complicated but doesn’t involve user-perceived  
downtime). It all boils down to what you and your users can live with.

In my experience, the only time I’ve ever done a redeployment that  
just contained a change to a lookup value was because of a typo. All  
other times I was changing logic at the same time so a redeployment  
was on the cards anyhow.

Trev

---

<div class="post-metadata">

**Author:** ![lkfken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lkfken](https://www.ruby-forum.com/u/lkfken)\
**Post date:** [May 11, 2006, 1:53am UTC](https://www.ruby-forum.com/t/how-best-to-implement-lookup-table/60135/11 "2006-05-11T01:53:09Z")

</div>

I have looked at your plugin a couple of times, but always stayed away  
because I thought it would make it difficult to change the lookup  
values.  
But I think you are right in noting that for many applications the  
lookup  
values won’t change that often. It would be ideal if there was a  
command  
you could issue to lock the application until changes to the lookups  
through  
an admin panel were complete. This way you could schedule your update,  
lock  
the application to prevent users from getting incorrect lookups during  
the  
update, make the changes through your application admin views, and then  
unlock the application for normal use again.

Do you have any views on this idea?
