Posted at

Rails 4.0.1以降へのアップデート後に`ARModel.create`が`NoMethodError: undefined method '[]' for nil:NilClass`というエラーを吐く

More than 5 years have passed since last update.


現象

Rails 4.0.1へアップデート後にsome_ar_base_class.createを呼ぶと以下のようなエラーが出るようになった.

[1] pry(main)> User.create

NoMethodError: undefined method `[]' for nil:NilClass
from /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/read.rb:86:in `block in read_attribute'

スタックトレースを見ても謎

     NoMethodError:

undefined method `[]' for nil:NilClass
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/read.rb:86:in `block in read_attribute'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/read.rb:84:in `fetch'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/read.rb:84:in `read_attribute'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/time_zone_conversion.rb:38:in `notification_last_seen='
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/evaluation.rb:12:in `object'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/factory.rb:42:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/notifications.rb:161:in `instrument'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/factory_girl-4.3.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# /Users/yaotti/src/qiita/spec/spec_helper.rb:99:in `foo'
# /Users/yaotti/src/qiita/spec/spec_helper.rb:62:in `block (2 levels) in <top (required)>'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:21:in `instance_eval'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:21:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:100:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:320:in `block in run_before_all_hooks'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/memoized_helpers.rb:96:in `block in isolate_for_before_all'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/memoized_helpers.rb:92:in `instance_eval'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/memoized_helpers.rb:92:in `isolate_for_before_all'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:319:in `run_before_all_hooks'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:370:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
# /opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'


解決策

protected_attributes gemを1.0.5以降にアップデートする.

関連しそうなコミット: Override the proper hook to make assign_attributes work with options · 22e1953 · rails/protected_attributes

Rails 3から4に移行した人はprotected_attributesを使っていることが多そうなのでメモ.