奇怪的问题has_many通过更新的Rails中的关联 [英] Weird issue has_many through association in updated Rails

查看:52
本文介绍了奇怪的问题has_many通过更新的Rails中的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Ruby 2.1.2更新到了Ruby 2.2.0,并更新了所有链接到它的gem.我有一些相互关联的模型,例如

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

如您所知,我们有一些问题可以解答,并通过answer_questions知道他们得到了什么.在我更新Ruby之前,它运行良好.现在当我做类似的事情...

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

它真的爆炸了

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表中的字段.我已经花了几个小时才了解这一点……

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 ...

推荐答案

仅供参考,我可以确认这是ActiveRecord和Ruby 2.2的问题.我使用的是ActiveRecord 3.2,由于更改为3-2-stable分支,因此问题不再存在.我相信现在在4.x分支中已解决此问题.我在 https://github.com/rails/rails/issues/18991上提出了一个问题.

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.

这篇关于奇怪的问题has_many通过更新的Rails中的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆