在一个模型导轨上连接或包含2个外键

时间:2015-02-21 19:17:27

标签: ruby-on-rails join include foreign-keys

我有一个会员应用程序,其中一个"参与者"有两个外键用于"用户" table,实际成员的id(user_id)以及"添加的用户的ID"会员(added_by)到" Club"。我正在尝试制作一种方法,列出所有的"俱乐部"给定用户(user_id)已由另一个用户(added_by)添加。我试图获得的文字是:

user.firstname (<-defined by user_id) has been added to Club.name by user.firstname (<-defined by added_by)

class ClubsController < ApplicationController

   @invites = Participant.find(current_user.id).includes(:user => foreign_key: :added_by).joins(:club)

以下是我的模特

class Participant < ActiveRecord::Base
    belongs_to :user, inverse_of: :participants
    belongs_to :user,  foreign_key: :added_by
    belongs_to :current_user

class Club < ActiveRecord::Base
 has_many :participants, dependent: :destroy, inverse_of: :club
 has_many :users, through: :participants

class User < ActiveRecord::Base
      has_many :participants, dependent: :destroy
      has_many :participants,  foreign_key: :added_by

0 个答案:

没有答案