【发布时间】:2015-01-21 07:05:46
【问题描述】:
我从 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 表中的字段。我已经花了几个小时试图理解这一点......
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord models