Develop Cancan Registration for Users and Companies

In a Rails 3 application, I have 2 logical entities - User and Company. I would like to have two different forms for registration (for users and for companies). It will also be great to have one login form for them.

Now I have a customized Devise + Cancan model for a user with two roles (user, company), so I now have "/ users / sign_in" and "/ users / sign_up".

I would like to have the following URLs in my application:

/login /users/signup /companies/signup 

Another question is how to organize the relationship between the user and the company, if the company inherited from the user or I use aggregation - User has_one Company? I prefer the second option and plan the user user.company with cancan user role = "company".

Please help me with this. Thanks.

0
source share
1 answer

You can create several models. You can also add a company.

rails generate devise company

This will give you the URL you specified.

Regarding the relationship between the user and the company. Generally accepted:

 class User < ActiveRecord::Base belongs_to :company end class Company < ActiveRecord::Base has_many :users end 

To migrate to the User model, you must add the company_id column for this to happen. Then there is no inheritance. Users and companies are treated completely separately. But you can access user.company and company.users .

+1
source

Source: https://habr.com/ru/post/1397387/


All Articles