我有两个模型,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
当我调用 QuestionEvent.first.questioner 或 QuestionEvent.first.respondent 时,我得到了预期的用户。但是,调用 User.first.asked_questions 给了我:
NoMethodError: undefined method `asked_questions' for #<User:0x007fc687d53ae0>
谁能看到我在这里犯了什么错误?
我的 QuestionEvent 数据库架构是:
t.integer :question_id
t.integer :questioner_id
t.integer :respondent_id