ruby-on-railsrubyassociationsactiveadmin

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.


Solution

  • 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