multiple user types with self references rails

298 views Asked by At

I am working on an app in rails where I need to make multiple types of users. I am using Devise gem for authentication. Now let me give you an overview what I am trying to achieve and could not for past whole week.

First, I want 2 types of users. Let us call them Client and Developer as shown below.

class Client < ActiveRecord::Base

end

class Developer < ActiveRecord::Base

end

Now, these 2 users have many to many relation with each other. A client can have many developers and a developer can also work for many clients. So, for that, we will need a middle table as well. Let us call it client_developer_join.

So, let us update our models accordingly.

class Client < ActiveRecord::Base
    has_many :developers, through: :client_developer_joins, dependent: :nullify
    has_many :client_developer_joins, dependent: :destroy
end

class Developer < ActiveRecord::Base
    has_many :clients, through: :client_developer_joins, dependent: :nullify
    has_many :client_developer_joins, dependent: :destroy
end

class ClientDeveloperJoin < ActiveRecord::Base
    belongs_to :client
    belongs_to :developer
end

Important:

Now, what I want is that when a user sign up, he will be given the ability to choose whether to sign up as a Client or a Developer using radio buttton or some other good looking thing. There will also be a third option named "Both". In that case, user will have the option to switch from Client to Developer and vice versa whenever he wants.

How can I acheive without creating multiple sign up and sign in forms for each type of user and give them the ability to switch.

I have looked into STI (Single Table Inheritance) and all the questions on StackoverFlow related to that but could not solve the problem.

One solution that I think is ideal but do not know how to achieve is to have seperate devise models for each type of user but that will create different sign up and sign in forms for that. I do not want that and it will also not give me the ability to switch from the one type to another type.

0

There are 0 answers