1

User と QuestionEvent の 2 つのモデルがあります。

    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
4

2 に答える 2

6

コンソールを再起動することで問題を解決したとおっしゃいました。それは問題ありませんが、より良い解決策は、コマンドを実行することreload!です。そうすれば、再起動する必要はありません。

于 2013-05-20T19:34:31.623 に答える
2

新しい変更を反映するには、Rails コンソールを再起動する必要があります。

于 2013-05-20T18:40:58.377 に答える