# handling multiple lookup tables in rails

**URL:** <https://discuss.rubyonrails.org/t/handling-multiple-lookup-tables-in-rails/32525>\
**Category:** rubyonrails-talk\
**Created:** [July 29, 2008, 7:23pm UTC](https://discuss.rubyonrails.org/t/handling-multiple-lookup-tables-in-rails/32525 "2008-07-29T19:23:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbl](https://avatars.discourse-cdn.com/v4/letter/g/3ab097/32.png) [@gbl](https://discuss.rubyonrails.org/u/gbl)\
**Post date:** [July 29, 2008, 7:23pm UTC](https://discuss.rubyonrails.org/t/handling-multiple-lookup-tables-in-rails/32525/1 "2008-07-29T19:23:10Z")

</div>

I've got an application that will have a handful of lookup tables. To be clear by this I mean tables that only have two attributes, id, name and are used to provide drop-down lists for other tables/forms so that these fields have "standard" values. I need 5 to 10 of these tables. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I'm a ruby/rails neophyte but I've read Agile Web Devel with Rails, Rails Recipes, Adv Rails Recipes so I think I understand the basics but this has me stumped. The models/views/controllers for all these lookup tables are identical. I would think there would be a way to have 1 Controller (e.g. LookupController) 1 set of views (in directory /lookups) and a model for each lookup table. Then the Lookup controller would (possibly with a before\_filter) figure out which lookuptable I want to edit (possibly with a route such as (lookup/:model) where :model would be the name of the lookup table I want to edit. &nbsp;&nbsp;&nbsp;&nbsp;Can anyone point me to either some documentation or provide hints on how to do this sort of thing cleanly? &nbsp;&nbsp;&nbsp;&nbsp;Below is the code I'm playing around with....

thanks

in routes I've got

# lookups/CollectionType/lookups #lookups/Gender/lookups

map.resources :lookups, :path\_prefix =\> 'lookups/:mdl'

ex Lookup Controller

class LookupsController \< ApplicationController &nbsp;&nbsp;# GET /lookup &nbsp;&nbsp;# GET /lookup.xml before\_filter :setup\_tables

&nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@lookups = @theModel.find(:all)

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookups } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# GET /lookup/1 &nbsp;&nbsp;# GET /lookup/1.xml &nbsp;&nbsp;def show &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookup } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# GET /lookup/new &nbsp;&nbsp;# GET /lookup/new.xml &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.new

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # new.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookup } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# GET /lookup/1/edit &nbsp;&nbsp;def edit &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.find(params[:id])

&nbsp;&nbsp;end

&nbsp;&nbsp;# POST /lookup &nbsp;&nbsp;# POST /lookup.xml &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.new(params[:lookup])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @lookup.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Lookup value was successfully created.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@lookup,:mdl=\>@mdl) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookup, :status =\> :created, :location =\> @lookup } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "new" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookup.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# PUT /lookup/1 &nbsp;&nbsp;# PUT /lookup/1.xml &nbsp;&nbsp;def update &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @lookup.update\_attributes(params[:lookup]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Lookup was successfully updated.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@lookup,:mdl=\>@mdl) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "edit" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @lookup.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;# DELETE /lookup/1 &nbsp;&nbsp;# DELETE /lookup/1.xml &nbsp;&nbsp;def destroy &nbsp;&nbsp;&nbsp;&nbsp;@lookup = @theModel.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;@lookup.destroy

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(lookups\_url, :mdl=\>@mdl) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { head :ok } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;private &nbsp;&nbsp;&nbsp;&nbsp;def setup\_tables &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I assume there is some way to instantiate a class with a string name but I did this &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# just to try to get something working &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:mdl] == "CollectionType" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@mdl = params[:mdl] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@theModel = CollectionType &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if params[:mdl] == "Gender" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@mdl = params[:mdl] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@theModel = Gender

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@lookupTbl = @theModel.find(:all) &nbsp;&nbsp;&nbsp;&nbsp;end end
