0

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.

Inzamam Tahir
  • 519
  • 4
  • 17
  • Hi for above scenario i prefer to create table role (ADMIN, USER..etc). At the time of registration user can select their role. it will be easy way to manage user . – Atul Shukla Dec 27 '16 at 11:58
  • Ok. user selects the roles at the time of registration and we save it as a type in db such as user_type: "Client".....Now, how can i say that user (as a client type) has many other users having user_type: "developer" ?? – Inzamam Tahir Dec 27 '16 at 12:00
  • Try Rolify gem in combination with Devise and CanCanCan. – bo-oz Dec 27 '16 at 14:25

0 Answers0