Possible solutions to the system of multiple users in Rails with Devise

0

I have been trying to create an ideal way to have multiple users in Rails. The idea is that there is only a single login and a single registration regardless of the number of user models.

The other option will be a single user model, but I do not want all the fields of all my models to be in one, since as you know there are fields in the "Company" model, which have nothing to do with the model "Client", or "Advisor" Model, so I would like that unique model, either called "User" or "Account", only have common fields and the other models the respective fields in each of them.

Possible solutions without success at the moment:

1.STI (Single Table Inheritance) It allows me to have only one table, and that the models of "Company", "Advisor", "Client", inherit from it, but it forces me to have all the fields in the model Father "User" or "Account" depending on his name, in the intention to put only common fields in the Parent model and put the fields in the respective models individually, it is impossible for me, since when inheriting from User, they can not interact in case alone with the BD, because the inheritance would be something like this: Class Enterprise < User, instead of how normal it would be: Class Enterprise < ActiveRecord :: Base

2.Polimorphism, this idea is great, however at the time of translating it, I had obviated that the company, client or adviser should already exist to make the connection with user, otherwise, userable will not be able to find the id and the type of the associated model. The idea is that the moment of the registration asks you what you want to be: "Company", "Client" or "Advisor", depending on what you request, a nested_form is displayed within the registration form, bringing the necessary fields from the user model to which I select, so that I continue completing the fields such as: "Type of company", "Address", "Telephone", etc. So in theory the company should also be created at the time of registering as User where only the fields of "Email, Password, Name" will be displayed and the nested will bring me the fields of the model to which reference is made, taking as an example the fields of the above mentioned company. But for this to work, the company must already exist. So you can take the userable_id, userable_type

I hope you can help me friends, greetings!

    
asked by Hector Hernandez 07.10.2016 в 18:23
source

1 answer

0

What you can do is create the model User which has a relationship with another model that can be "Client", "Advisor" or "Company". As follows:

User.rb

class User < ActiveRecord::Base
 belongs_to :client
 belongs_to :enterprise
 belongs_to :asesor

 def entidad
  if self.tipo == "Client"
    self.client
  end
  if self.tipo == "Enterprise"
    self.enterprise
  end
  if self.tipo == "Asesor"
    self.asesor
  end
  null
 end

end

Example of Client.rb, in the same way would be the Enterprise.rb and the Asesor.rb

class Client < ActiveRecord::Base
 has_one :user
end

So you can access it like this:

user.entidad

or

client.user

PS: Another good option is Polymorphous Association but I do not know if it goes well in this case, however, I recommend you check it out.

    
answered by 12.10.2016 в 21:25