raails 4.0.1中的column_types为nil

 长春搜房-杨志玲 发布于 2023-02-10 12:36

我正在尝试将Rails从4.0.0升级到4.0.2,但是4.0.1中引入的更改破坏了我的应用程序.

这是我看到的错误:

undefined method `[]' for nil:NilClass

activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:86:in `block in read_attribute'
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `fetch'
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `read_attribute'
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:59:in `__temp__6696273747f5e616d656'
app/models/player.rb:20:in `short_name'

这是堆栈跟踪开始的应用程序代码:

# app/models/player.rb
def short_name
  self.class.where(first_name: first_name).where('id != ?', id).exists? ? name : first_name
end

这是出现问题的差异(和Rails上的提交):

diff -r activerecord-4.0.0/lib/active_record/attribute_methods/read.rb activerecord-4.0.1/lib/active_record/attribute_methods/read.rb

[snip]

80,86c85,92
<           column = @columns_hash.fetch(name) {
<             return @attributes.fetch(name) {
<               if name == 'id' && self.class.primary_key != name
<                 read_attribute(self.class.primary_key)
<               end
<             }
<           }
---
>           column = @column_types_override[name] if @column_types_override
>           column ||= @column_types[name]
> 
>           return @attributes.fetch(name) {
>             if name == 'id' && self.class.primary_key != name
>               read_attribute(self.class.primary_key)
>             end
>           } unless column

根据堆栈跟踪,@column_types是零,我无法弄清楚为什么.

我在控制台上没有这个问题,只有当我在服务器上运行时,无论是Thin via rails s(开发环境)还是Unicorn via foreman start(生产环境).当我部署到Heroku上的暂存时,它也会失败.我也有测试覆盖这个方法,他们通过了.

有谁知道@column_types应该设置在哪里以及为什么在这种情况下它是零?

1 个回答
  • 我也打了这个 - 你需要将protected_attributesgem 更新到版本1.0.5.

    这里有更多信息:https://github.com/rails/rails/issues/13246


    从OP编辑:您可能只需要刷新缓存,该线程中提到了:

    Rails.cache.clear
    

    2023-02-10 12:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有