Implementing a lookup table in Rails

痞子三分冷 提交于 2019-12-06 01:43:10

问题


I'm currently using (or trying to use) a lookup table to represent a column that contains an enumerated string. I've had extensive discussions with my team to make sure this is the direction we want to go and have decided to pursue it but unfortunately none of us know how to tie the lookup table to the column using migrations and if there are changes that have to be made in the model/controller/views to support this new behavior.

I've googled this extensively and have not found good examples of doing this so I'm hoping someone can either tell me of a wonderful link to a tutorial I just missed in my search, or describe how to do this. I also would love to know if there is an unofficial (or official) Railsy pattern for this.

Thanks in advance.


回答1:


I encountered the same problem as you, and solved it by creating a gem that adds a dynamic lookup table transparently into the model. I've blogged about it here: 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




回答2:


If you're looking for a light-weight way to model enumerations/configuration without creating a separate table, definitely check out ActiveHash:

https://github.com/zilkey/active_hash




回答3:


Do you really need it to be in the database?

If it is just a short list, a static array (loaded from a CSV file) might be enough.



来源:https://stackoverflow.com/questions/4828568/implementing-a-lookup-table-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!