繁体   English   中英

奇怪的问题has_many通过更新的Rails中的关联

[英]Weird issue has_many through association in updated Rails

我从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 2.2.0时遇到了同样的问题。 对于特定的项目,我回到了以前的版本2.1.3,一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM