# NoobyQ: how to work with a table of static lookup data?

**URL:** <https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521>\
**Category:** Rails\
**Created:** [January 2, 2006, 6:31am UTC](https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521 "2006-01-02T06:31:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmarmer1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rmarmer1](https://www.ruby-forum.com/u/rmarmer1)\
**Post date:** [January 2, 2006, 6:31am UTC](https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521/1 "2006-01-02T06:31:55Z")

</div>

Hello out there!

Nooby question: What’s the “Rails” way to work with tables of relatively  
static lookup data?

Objectively (I’m thinking); I don’t want to hit the DB each time, so for  
each lookup table, initialize a globally available hash with lable/value  
pairs…

Am I on the right track?

How would you do this kind of thing in Rails?

Thanks!

---

<div class="post-metadata">

**Author:** ![rmarmer1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rmarmer1](https://www.ruby-forum.com/u/rmarmer1)\
**Post date:** [January 2, 2006, 11:39am UTC](https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521/2 "2006-01-02T11:39:29Z")

</div>

Randy,

Being a newby I see some possibilities. You could set up caching (I’ve  
read  
that somewhere). Or you could put it in an array (in a controller if  
it’s not  
to big) or as a flat text file.  
I think it depends on in what format you get the information in the  
first  
place, and how often it’s updated

Ik hope (wonder if) it helps.

Regards,

Gerard.

On Monday 02 January 2006 06:31, randy marmer tried to type something  
like:

> How would you do this kind of thing in Rails?
> 
> Thanks!

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![rmarmer1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rmarmer1](https://www.ruby-forum.com/u/rmarmer1)\
**Post date:** [January 2, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521/3 "2006-01-02T19:19:59Z")

</div>

On Mon, Jan 02, 2006 at 06:31:55AM +0100, randy marmer wrote:  
} Nooby question: What’s the “Rails” way to work with tables of  
relatively  
} static lookup data?  
}  
} Objectively (I’m thinking); I don’t want to hit the DB each time, so  
for  
} each lookup table, initialize a globally available hash with  
lable/value  
} pairs…  
}  
} Am I on the right track?  
}  
} How would you do this kind of thing in Rails?

Assuming you have your database connections set up properly (in  
config/database.yml) you should be able to use the ActiveRecord  
connection.  
Assume that your DB table name is Lookup\_Data with Lookup\_Key and  
Lookup\_Value columns:

$lookup\_data = {}  
ActiveRecord::Base.connection.select\_all(‘SELECT Lookup\_Key,  
Lookup\_Value  
FROM Lookup\_Data’).each { |row|  
$lookup\_data[row[‘lookup\_key’]] = row[‘lookup\_value’]  
}

Note that the capitalization in the row column names may vary depending  
on  
which database driver you are using. I tested this with the PostgreSQL  
driver, and it insists on the column names being lowercase.

} Thanks!  
–Greg

---

<div class="post-metadata">

**Author:** ![rmarmer1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rmarmer1](https://www.ruby-forum.com/u/rmarmer1)\
**Post date:** [January 2, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/noobyq-how-to-work-with-a-table-of-static-lookup-data/51521/4 "2006-01-02T22:20:49Z")

</div>

Gregory S. wrote:

> $lookup\_data = {}  
> ActiveRecord::Base.connection.select\_all(‘SELECT Lookup\_Key,  
> Lookup\_Value  
> FROM Lookup\_Data’).each { |row|  
> $lookup\_data[row[‘lookup\_key’]] = row[‘lookup\_value’]  
> }

Thanks Greg - & Gerard, thank you too - for responding…

Greg, the approach you suggest seems quite reasonable… But I’m  
wondering where it belongs? The lookup tables that I’m referencing are  
relatively static, so it would not be necessary to load them very often.

Would this type of thing go into the initialize method of the  
application controller? Frankly, I’m still confused about how it’s Rails  
uses the application controller in practice… Would its initialize  
method be called once per session (i.e. per browser)? If so then that  
would seem to be the most logical spot to place this kind of code… ?  
Am I more or less on track or is there a more standard “Rails approach”  
to pull this off?

Thanks again!
