I’m trying to set up a self-referential relationship, as described in this great video – http://railscasts.com/episodes/163-self-referential-association – and it’s mostly working, but not entirely working.
I have these entities: Users, who can either be a mentor or a mentee; Matches, which have a mentor_id (user.id), mentee_id (user.id), and status_id; and Statuses, which are a plain lookup table.
My User model looks like this:
class User < ActiveRecord::Base
has_many :matches
has_many :mentors, :through => :matches
has_many :mentees, :through => :matches
has_many :statuses, :through => :matches
end
My Status model looks like this:
class Status < ActiveRecord::Base
has_many :matches
end
My Match model looks like this:
class Match < ActiveRecord::Base
belongs_to :mentor, :class_name => "User"
belongs_to :mentee, :class_name => "User"
belongs_to :status
end
When I puts user.mentors I get SQLite3::SQLException: no such column: matches.user_id: SELECT “users”.* FROM “users” INNER JOIN “matches” ON “users”.id = “matches”.mentor_id WHERE ((“matches”.user_id = 1))
Simply, I was hoping to do user.matches.find(1).status.id .. Any ideas on what I’m doing wrong?
Thanks in advance.
Jon
I’m pretty sure you’re looking for:
Which gives:
EDIT, to associate a mentee:
Rails will handle everything properly.
To know which is the relation between a mentor and one of his mentees:
Of course, you can create the same for mentor relationship