Search This Blog

Friday, August 12, 2011

NoMethodError (undefined method `eq' for nil:NilClass):

Context: Rails 3 app
Problem: when trying to save! an instance of the join model in a has_many :through I was getting the following error:

NoMethodError (undefined method `eq' for nil:NilClass):
  app/models/user.rb:29:in `update_achievement'
  app/controllers/game_controller.rb:183:in `check_point'
  app/controllers/game_controller.rb:177:in `each'
  app/controllers/game_controller.rb:177:in `check_point'

The Solution:

The has_many :through was originally a has_and_belongs_to_many relation between the outer models, but since I needed to store more data in the join model I switched to a has_many :through.  The problem was that while I correctly set the relation in Rails, I forgot to add the id column in the join model. Adding the id column solved the problem.

1 comment:

  1. I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best posting.