1

この素晴らしいビデオ ( 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を実行したいと思っていました..私が間違っていることについて何か考えはありますか?

前もって感謝します。ジョン

4

3 に答える 3

4

私はあなたが探していると確信しています:

class User < ActiveRecord::Base
  has_many :match_mentors, :foreign_key => "mentee_id", :class_name => "Match"
  has_many :match_mentees, :foreign_key => "mentor_id", :class_name => "Match"

  has_many :mentors, :through => :match_mentors
  has_many :mentees, :through => :match_mentees

  def status_with_mentee mentee_id
    match = match_mentees.find_by_mentee_id(mentee_id)
    return nil if match.nil? 
    match.status
  end
end


class Match < ActiveRecord::Base
  belongs_to :mentor, :class_name => "User"
  belongs_to :mentee, :class_name => "User"
  belongs_to :status

  after_create :set_status

  def set_status
   Status.find(1).matches << self #replace 1 with the id you want
  end
end

これにより、次のことが得られます。

u = User.create
u.mentors               => [] 
u.mentors.create        => #<User id: 2, name: nil, created_at: "2011-06-17 19:02:17", updated_at: "2011-06-17 19:02:17"> 
u.mentors               => [#<User id: 2, name: nil, created_at: "2011-06-17 19:02:17", updated_at: "2011-06-17 19:02:17">] 
v = u.mentors.first     => #<User id: 2, name: nil, created_at: "2011-06-17 19:02:17", updated_at: "2011-06-17 19:02:17"> 
v.mentees               => [#<User id: 1, name: nil, created_at: "2011-06-17 18:58:44", updated_at: "2011-06-17 18:58:44">] 

編集、メンティーを関連付ける:

current_user.mentees << User.find(params[:mentee_id])

Rails はすべてを適切に処理します。

メンターとメンティーの 1 人との関係を知るには、次のようにします。

current_user.status_with_mentee(params[:mentee_id])

もちろん、メンター関係にも同じものを作成できます

于 2011-06-17T19:06:07.517 に答える
1

user.mentorsを使用するには、

belongs_to :user 

Match モデルで、どのユーザーに関連付けられているかを認識できるようにします。

(マッチテーブルには user_id も必要です)

于 2011-06-17T14:55:18.720 に答える
0

問題は、モデルが取得するレコードがUserわからないことです。モデルでは、それを伝えるMatchので、テーブルに列があると想定します。しかし、あなたのテーブルにはとしかありません。Userhas_many :matchesuser_idmatchesmatchesmentor_idmentee_id

何をしようとしているのかを私たちに伝える必要があります。メンターであるかメンティーであるかに関係なく、ユーザーが登場するすべてのマッチを取得しますか? それとも、彼がメンターである場合にのみ、使用は一致を「持っている」のでしょうか? それともメンター?

考えられる解決策

User.first.matchesメンティーとメンターの両方として、ユーザーが登場するすべての一致を返す必要がある場合は、これが可能な解決策です。

class User < ActiveRecord::Base
  def matches
    Match.where("mentor_id = :id OR mentee_id = :id", { :id => self.id })
  end

  def mentors
    User.joins("INNER JOIN matches ON matches.mentor_id = users.id").where("matches.mentee_id = ?", self.id)
  end

  def mentees
    User.joins("INNER JOIN matches ON matches.mentee_id = users.id").where("matches.mentor_id = ?", self.id)
  end
end

他のモデルはそのままにしておいてください。User.first.matchesこれにより、User.first.matches.find(1).status.idUser.first.mentors.first.menteesなどを実行できるようになります。

関連付けを維持し、ユーザーがメンターとメンティーの両方として表示される一致を取得する方法を誰かが知っている場合は、私もそうしたいと思います。

于 2011-06-17T15:35:14.957 に答える