User Types, Properties and Routes

I have an application with multiple user types, internal, external and a couple more to come. They share only the authentication properties, but after that they vary significantly.

I was thinking of using a polymorphic association on the User with the models, something like what Derek had here: I would have ExternalProfile and InternalProfile to start off with. An ExternalProfile would have relationships that an InternalProfile would not have. So I could build those relationships off of those models instead of directly on the User model. Good/bad? Any better ideas?

I also have security and route needs based on the profile type. I can work through the security using pundit, but how would you approach different routes for different user types? Specifically the root route for a user type. Two possibilities I can think of:

  • Just a simple case statement in a RootRoutingController that directs them correctly based on their type.
  • Each profile holds onto their root route using Rails.application.routes.url_helpers, then I could just redirect to profile.root_path

Neither one of these seems exciting to me. The first one, I have to open up the RootRoutingController every time I add a user type. The second one has my model having router logic inside it.

I am sure I am missing something and thought someone would be able to point me in a different direction altogether or maybe just on a part of it.

Did you make any progress on this? I’m facing a similar issue.

The system that has grown in my app to deal with this has started to break down and I need a more robust solution.

I haven’t used them much, but take a look at routing constraints to see if you can handle things with that:

1 Like