Implementing a lookup table in Rails

I am currently using (or trying to use) a lookup table to represent a column containing an enumerated row. I had extensive discussions with my team to make sure that this is the direction we want to go, and decided to continue it, but, unfortunately, none of us knows how to associate a lookup table with a column using migration, and if There are changes that need to be made in the model / controller / views to support this new behavior.

I studied this problem extensively and did not find any good examples for this, so I hope someone can tell me a wonderful link to a tutorial that I just skipped in my search, or describe how to do this. I would also like to know if there is an unofficial (or official) Railsy image for this.

Thanks in advance.

+6
ruby ruby-on-rails lookup-tables
source share
3 answers

I ran into the same problem as you, and solved it by creating a gem that transparently adds a dynamic lookup table to the model. I wrote about this on my blog: http://www.codelord.net/2011/08/09/guest-post-lookup-tables-with-ruby-on-rails/

And the code is here: https://github.com/Nimster/RailsLookup

+3
source share

If you are looking for an easy way to model enumerations / configurations without creating a separate table, be sure to check ActiveHash:

https://github.com/zilkey/active_hash

+5
source share

Do you really need to be in the database?

If this is just a short list, a static array (loaded from a CSV file) may be sufficient.

0
source share

All Articles