【问题标题】:Weird issue has_many through association in updated Rails奇怪的问题 has_many 通过更新的 Rails 中的关联
【发布时间】: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


    【解决方案1】:

    仅供参考,我可以确认这是 ActiveRecord 和 Ruby 2.2 的问题。我使用的是 ActiveRecord 3.2,自从更改为 3-2-stable 分支后,问题就消失了。我相信这现在在 4.x 分支中得到了修复。我已经在https://github.com/rails/rails/issues/18991 提出了一个问题。

    【讨论】:

      【解决方案2】:

      我在使用 ruby​​ 2.2.0 时遇到了同样的问题。对于具体的项目,我回到了我之前的版本,2.1.3,一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 2010-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多