在 Rails 中实现查找表 [英] Implementing a lookup table in Rails

查看:62
本文介绍了在 Rails 中实现查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用(或尝试使用)查找表来表示包含枚举字符串的列.我与我的团队进行了广泛的讨论,以确保这是我们想要走的方向,并决定追求它,但不幸的是,我们都不知道如何使用迁移将查找表与列联系起来,以及是否有更改在模型/控制器/视图中进行以支持这种新行为.

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.

我已经在谷歌上广泛搜索了这个,但没有找到这样做的好例子,所以我希望有人可以告诉我一个我在搜索中遗漏的教程的精彩链接,或者描述如何做到这一点.我也很想知道是否有一个非官方(或官方)的 Railsy 模式.

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.

提前致谢.

推荐答案

我遇到了和你一样的问题,并通过创建一个 gem 来解决它,该 gem 透明地将动态查找表添加到模型中.我在这里写了博客:http://www.codelord.net/2011/08/09/guest-post-lookup-tables-with-ruby-on-rails/

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/

代码在这里:https://github.com/Nimster/RailsLookup

这篇关于在 Rails 中实现查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆