この素晴らしいビデオ ( http://railscasts.com/episodes/163-self-referential-association ) で説明されているように、私は自己参照関係を設定しようとしていますが、ほとんどは機能していますが、完全には機能していません。
私はこれらのエンティティを持っています: メンターまたはメンティーのいずれかになることができるユーザー。mentor_id (user.id)、mentee_id (user.id)、および status_id を持つ一致。ステータスは単純なルックアップ テーブルです。
私の User モデルは次のようになります。
class User < ActiveRecord::Base
has_many :matches
has_many :mentors, :through => :matches
has_many :mentees, :through => :matches
has_many :statuses, :through => :matches
end
私の Status モデルは次のようになります。
class Status < ActiveRecord::Base
has_many :matches
end
私のマッチモデルは次のようになります。
class Match < ActiveRecord::Base
belongs_to :mentor, :class_name => "User"
belongs_to :mentee, :class_name => "User"
belongs_to :status
end
user.mentorsを入れるとSQLite3::SQLException: no such column: matches.user_id: SELECT "users".* FROM "users" INNER JOIN "matches" ON "users".id = "matches".mentor_id WHERE ( (「一致」.user_id = 1))
簡単に言えば、私はuser.matches.find(1).status.idを実行したいと思っていました..私が間違っていることについて何か考えはありますか?
前もって感謝します。ジョン