column_types is nil in Rails 4.0.1

依然范特西╮ 提交于 2019-12-23 16:10:34

问题


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.

Here's the error I'm seeing:

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

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

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

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.

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


回答1:


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

There's a bit more info here: https://github.com/rails/rails/issues/13246


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

Rails.cache.clear


来源:https://stackoverflow.com/questions/20596842/column-types-is-nil-in-rails-4-0-1

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