has_many关联中的未定义方法错误

Nea*_*ung 1 ruby web-applications ruby-on-rails

我有两个模型,User和QuestionEvent.

    class User < ActiveRecord::Base
    ...
    has_many :questions
    has_many :asked_questions, :class_name => 'QuestionEvent', :foreign_key => 'questioner_id'
    has_many :received_questions, :class_name => 'QuestionEvent', :foreign_key => 'respondent_id'
    end

    class QuestionEvent < ActiveRecord::Base
    ...
    belongs_to :question  
    belongs_to :questioner, :class_name => 'User'
    belongs_to :respondent, :class_name => 'User'
    end
Run Code Online (Sandbox Code Playgroud)

当我调用QuestionEvent.first.questioner或QuestionEvent.first.respondent时,我得到了预期的用户.但是,调用User.first.asked_questions给了我:

NoMethodError: undefined method `asked_questions' for #<User:0x007fc687d53ae0>
Run Code Online (Sandbox Code Playgroud)

谁能看出我在这里犯的是什么错误?

QuestionEvent的数据库模式是:

  t.integer :question_id
  t.integer :questioner_id
  t.integer :respondent_id
Run Code Online (Sandbox Code Playgroud)

Nic*_*ick 6

你说你通过重启控制台解决了你的问题.这很好,但更好的解决方案是运行命令reload!.这样,您不必重新启动.