Search code examples
ruby-on-railsdeviseauthorizationcancan

A Rails app where the User is also a "something else", not sure how to word this correctly


I am working on a Rails app where the data model involves the following:

  • Companies, which have_many Restaurants
  • Restaurants, which have_many Reservations
  • Customers, which mave_many Reservations

My confusion comes from the fact that there are 3 distinct types of users:

  • An employee of the Company, who can see an admin dashboard showing data on all of the restaurants that company owns/manages
  • The restaurant itself (which will, in theory, have their dashboard open all day, and should be able to log into their own dashboard, but not be able to see any other restaurant's dashboard)
  • The customer, who has a UI to make a reservation at a certain restaurant

Should the Restaurant be a type of User?

Should each restaurant just get it's own standard user-login to access their specific restaurant? If this is the case, would a Restaurant have_one User, and I can use something like CanCanCan to restrict Users so that they can only access a Restaurant where the Restaurant's ID == User.restaurant.id?

This is my first app that addressed atypical User types, so I'm at a complete loss on this. Any guidance/best practices on how to address a situation like this would be much appreciated!

Additionally, I would like to use Devise for the User model(s). Thanks!


Solution

  • I would say, first of all, that it is only ever a User that is actually logging into your website. A restaurant can't log into a website. A user who is a representative of the restaurant can log in. Therefore the restaurant should not be a type of user.

    A better fit is to give your users roles, and have one of the roles be "restaurant_manager" or something. These users would naturally be associated with the restaurant too, so your code could look something like

    if current_user.role == "restaurant_manager"
      #show extra links for the restaurant admin section
    elsif current_user.role == "company_manager"
      #show extra links for the company admin section
    

    or something along those lines, and, like you suggest, you make sure that a user can only ever access their own restaurant/company in the restaurant/company admin sections.