简体   繁体   中英

Rails App with 3 different types of Users

I'm now in the design phase of application. Mostly doing stuff on the paper to get the insight of the application models, define what I will need and so on. And I found there one problem, which I don't know how to solve, or be more specific, I don't know how to implement it to keep it dry. So here is the deal:

I have application where will be three different types of users/accounts:

  1. Regular account - Account has basic profile with basic rights in the application, User with regular account can see everyone's public profile, can modify own profile, can have only 1 profile picture, can search, etc.

  2. Advanced account - has advanced profile, which means that user can specify contact informations, appearance detail, has booking page, can get offers, have calendar

  3. Company account - has company profile which contains contact information for company, can make offers to Advanced users and can browse all profiles in the application

So basically, I have one User model together for all my Users and I decided to go with Devise for the authentication. After the User is created I want to show him/her a screen where he/she can choose account type - Regular, Advanced, Company. And after that he/she will have appropriate rights in the system and also appropriate profile. He will not be able to change between the account type in the future.

I want also keep the code clean and dry and I'm quite struggle with finding the best solution for. And now I banging my head against the wall, because I can't figure out how to do it. Here is the question:

How should look the final model diagram for this. Should it be polymorphic association? OR should it be just general:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :account_type

  has_one :account
  has_one :entertainer
  has_one :company
  after_create :create_account

  def create_account
    if self.account_type == 'Advanced'
      self.entertainer = AdvancedAccount.create(user_id: self.id)
    elsif self.account_type = "Company"
      self.company = CompanyAccount.create(user_id: self.id)
    else
      self.account = RegularAccount.create(user_id: self.id)
    end   
  end
end

Or I'm on the totally wrong path? What are the best practices for this matter? Thank you for your advices!

Instead of managing separate models for the different types of user, I've found the cancan gem to be very useful for managing user abilities and authorization. I used this blog post in the past to set it up with devise, though it may be fairly outdated by now.

I would use a few methods inside your UsersController to define what the various access levels are, for example:

@user = is_advanced?

This way you can allow them to choose the type of account they will have, store that selection in the database [users::account_type: integer] . This will allow you to let them change n the future if you decide to add that functionality.

I wouldn't create so many associations in your Users class, because it will bloat your RESTfull routes, etc and make debugging down the road a nightmare.

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