NoMethodError: undefined method `name' for nil:NilClass
has_manyで紐付けしているモデル間の挙動でエラーが発生した際の対応メモ
環境
エラー内容
NoMethodError: undefined method `name' for nil:NilClass
原因
Ruby2.2とRails4.1の環境ではActiveRecordにバグがある(らしい)
I think there is a bug in activerecord 4.1.0 with has_cached_counter? and ruby 2.2. It was fixed on activerecord 4.1.2
対応
Railsのバージョンを上げて再起動しました。
- Gemfile
gem 'rails', '4.1.2'
$ bundle update rails $ rails -v Rails 4.1.2
無事に動くことを確認しました。