我正在尝试建立一个自我参照关系,如这段精彩的视频中所述 - http://railscasts.com/episodes/163-self-referential-association - 它主要是有效的,但并不完全有效。
我有这些实体: 用户,可以是导师或被指导者;匹配项,有一个mentor_id(user.id)、mentee_id(user.id)和status_id;和状态,这是一个简单的查找表。
我的用户模型如下所示:
class User < ActiveRecord::Base
has_many :matches
has_many :mentors, :through => :matches
has_many :mentees, :through => :matches
has_many :statuses, :through => :matches
end
我的状态模型如下所示:
class Status < ActiveRecord::Base
has_many :matches
end
我的 Match 模型如下所示:
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 .. 关于我做错了什么的任何想法?
提前致谢。乔恩