我从 Ruby 2.1.2 更新到 Ruby 2.2.0 并更新了所有链接到它的 gem。我有几个模型相互关联,例如
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
正如您所理解的,我们的问题得到了答案,并通过 answer_questions 来了解他们得到了什么。在我更新 Ruby 之前,它工作得很好。现在当我做类似...
my_question.answers << my_answer
它真的爆炸了
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
是应该在 questions
表中的字段。我已经花了几个小时来理解这个......
最佳答案
仅供引用,我可以确认这是 ActiveRecord 和 Ruby 2.2 的问题。我使用的是 ActiveRecord 3.2,自从更改为 3-2-stable 分支后,问题就消失了。我相信这现在已在 4.x 分支中修复。我已经在 https://github.com/rails/rails/issues/18991 提出了这个问题.
关于ruby-on-rails - 奇怪的问题 has_many 通过更新的 Rails 中的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28061409/