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)
| 归档时间: |
|
| 查看次数: |
2171 次 |
| 最近记录: |