问题
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 ...
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/28061409/weird-issue-has-many-through-association-in-updated-rails