简体   繁体   中英

Devise Model inherit from another devise model

I'm creating an online real estate platform which facilitates (normal) Users and Real Estate agencies. This platform has following user types:

  • Normal Users: can search houses for rent/purchase, reviews agencies etc.
  • Agencies: can upload posts for house rent/sell etc
  • Employee: works for a Agencies
  • Super Admin: manages the site
  • Team Member: manages portions of the site delegated by Super Admin

Each user type then has different fields (4 - 6 additional fields each). Normal Users and Agencies can self-register. Employees and Admins can be registered by Agencies and Super Admins respectively. Furthermore there are some different multiple roles for a user type (which is not relevant to discuss for this case).

Currently, I have three different devise models (User, Agencies & Super Admin). They have different login and registration forms but share same session [Reference: [ https://github.com/plataformatec/devise/wiki/How-to-Setup-Multiple-Devise-User-Models]][1] User has facility to register via social media (OmniAuth) but Agency must has to register via private Email Id. And Admins will only be registered by Super Admins.

Here comes requirement [PROBLEM]:

The following fields are what I've identified as being shared between the three user types above: Email address & Username They must be unique for all cases. For example, if an agency has a username then user can't register with same username. At the moment, we are not meeting above requirement as we have different tables/models for different type of user.

MY PLAN

Now, since these are all really different users who interact with different sections of the application, so it feels clean to have three different devise models. Therefore, what I think is to create a parent devise model which should be inherited by all type of user models. This way can help me to solve my problem. But now, I'm not confident that how can I inherit parent devise model and use it for my case.

Second option is to use STI (Single Table Inheritance). But my major concern with this approach is that it will create mess with registration and security things.

Now here is my problem with my researched options. What would you recommend me to chose. Moreover, it'd be great if you put an example for elaboration.

Ps I am inclined to the first one option as I have already done with three different models and their login/registration stuff. :)

Not sure how your going to accomplish this without a roles system.. I suppose you could namespace or scope it out but wouldnt be the most secure to keep access to a specific user group.. atleast in my oppinion.

You could Use Single Table Inheritance (STI)..

Essentially you could do something like this (if i understand you correctly)

class User < ApplicationRecord
 # MASTER
end

class AnotherUser < User
 # Inherits attributes from User Table
end

going this route you will need to add type:string to your User model.

here are a few reference links:

http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html http://eewang.github.io/blog/2013/03/12/how-and-when-to-use-single-table-inheritance-in-rails/

they helped me when i ventured down the STI path..

For example, In a project I did this like so:

class User < ApplicationRecord
  # Devise Modules Here
end

class Admin < User

end

class SuperAdmin < User

end

now the tricky part I found was creating these users.. i opted to create a controller and a link, so in my app a User is created only by an admin or superadmin, and an admin can only be created by a superadmin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM