在 Rails 4.0.1 中 column_types 为零 [英] column_types is nil in Rails 4.0.1

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

问题描述

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

I'm attempting to upgrade Rails from 4.0.0 to 4.0.2, but there's a change that was introduced in 4.0.1 that's breaking my application.

这是我看到的错误:

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'

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

Here's my application code where the stack trace starts:

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

这是似乎存在问题的差异(和 Rails 上的提交):

Here's the diff (and the commit on Rails) that appears to be the problem:

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 为零,我不知道为什么.

According to the stack trace, @column_types is nil, and I can't figure out why.

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

I don't have this problem from the console, only when I'm running in a server, either Thin via rails s (development environment) or Unicorn via foreman start (production environment). It also fails when I deploy to staging on Heroku. I have tests covering this method too, and they pass.

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

Does anyone know where @column_types is supposed to be set and why it's nil in this case?

推荐答案

我也刚碰到这个 - 你需要将 protected_attributes gem 更新到 1.0.5 版.

I just hit this too - you'll need to update the protected_attributes gem to version 1.0.5.

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

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

Edit from OP: You may only need to flush your cache, which is mentioned in that thread:

Rails.cache.clear

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

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