我正在尝试将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
应该设置在哪里以及为什么在这种情况下它是零?
我也打了这个 - 你需要将protected_attributes
gem 更新到版本1.0.5.
这里有更多信息:https://github.com/rails/rails/issues/13246
从OP编辑:您可能只需要刷新缓存,该线程中提到了:
Rails.cache.clear