本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7596595
建立时间:2006年5月29日




[Ruby on Rails]Rails - Easily work with uppercase column names
软件技术

lhwork 发表于 2007/1/19 15:59:20

我们在Oracle建的表和字段名字基本上是大写的。这对我们是非常有用的。 I had to work with a legacy database with hundreds of tables with uppercase column names. Here is how I made my life a whole lot easier... 代码 module ActiveRecord      class MyCustomARClass < ActiveRecord::Base              def self.reloadable?          false       end                # all columns are uppercase        set_primary_key 'ID'                # convert to uppercase attribute if in existence        # record.name => record.NAME        # record.id => record.ID        def method_missing(method_id, *args, &block)          method_id = method_id.to_s.upcase if @attributes.include? method_id.to_s.upcase.gsub(/=/, '')          super(method_id, *args, &block)        end           # strip leading and trailing spaces from attribute string values        def read_attribute(attr_name)          value = super(attr_name)          value.is_a?(String) ? value.strip : value        end           class << self # Class methods                    private           # allow for find_by and such to work with uppercase attributes            # find_by_name => find_by_NAME            # find_by_dob_and_height => find_by_DOB_and_HEIGHT            def extract_attribute_names_from_match(match)              match.captures.last.split('_and_').map { |name| name.upcase }            end        end      end    end   最后更新:2007-01-08 07:58


阅读全文(1625) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144777276 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号