简体   繁体   中英

How can i self reference user in another table ruby on rails

Hi i need to have a manager for my user table. I have now created an intermediate table called liaison which will contain manager_id and references user_id table. The problem is that i don't know how to add the association between them and i need to list the users as manager in my activeadmin panel also during user creation.

I have tried this association in user.rb

  has_many :liaisons
  has_many :managers, through: :liaisons

In liaison.rb

  has_many :users
  has_many :users, through: :liaisons

My liaisons table contains manager_id and user_id i need to create multiple managers for a single user.

I need to list the all the users as managers during creation of user and associate them. Thanks.

The liaisons model only references a single user/manager and needs to know to map managers back to the users table:

class Liaison < ApplicationModel
    belongs_to :user
    belongs_to :manager, class_name: "User"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM