Laurent
Laurent

Reputation: 2178

Weird issue has_many through association in updated Rails

I updated from Ruby 2.1.2 to Ruby 2.2.0 and updated all the gems linked to it. I've a few models linked between each others like

class Question < ActiveRecord::Base

      belongs_to :course_version  
      has_one :course, through: :course_version


      has_many :answer_questions
      has_many :answers, through: :answer_questions

end

class AnswerQuestion < ActiveRecord::Base

      belongs_to :answer
      belongs_to :question

end

class Answer < ActiveRecord::Base

      has_many :answer_questions
      has_many :questions, through: :answer_questions

end

As you understood, we have questions that got answers and go through answer_questions to know what they got. It was working perfectly before I updated Ruby. Now when i do something like ...

my_question.answers << my_answer

It literally blows up

NoMethodError: undefined method `name' for nil:NilClass
    /Users/Loschcode/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:80:in `cached_counter_attribute_name'
    /Users/Loschcode/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:76:in `has_cached_counter?'
    /Users/Loschcode/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:84:in `update_counter'

name is a field which's supposed to be in the questions table. It's been a couple of hour I try to understand this ...

Upvotes: 2

Views: 587

Answers (2)

Matthew O&#39;Riordan
Matthew O&#39;Riordan

Reputation: 8211

FYI, I can confirm this is an issue with ActiveRecord and Ruby 2.2. I was using ActiveRecord 3.2 and since changing to the 3-2-stable branch, the issue is gone. I believe this is fixed now in 4.x branches. I have raised an issue for this at https://github.com/rails/rails/issues/18991.

Upvotes: 4

user3590393
user3590393

Reputation: 56

I had the same problem using ruby 2.2.0. For the specific project, I returned to my previous version, 2.1.3, and everything goes right.

Upvotes: 2

Related Questions