In my Rails application with Devise, I plan to make multiple types of Users (Student, Teacher, and Admin). They have a number of shared attributes such as username, email, password, etc, but with some differences. Students will be able to interact with the content posted by teachers (and probably each other), Teachers can make classes and post content and interact with their students, and Admin will likely have control over most everything (including taking down users, content, etc.)
I am trying to figure out the best way to do this, and preferably with the least headaches as I am still new to Rails. Should I make multiple Devise Models, one for each type of User, or should I make multiple models inherit from User (which I believe is called Single table inheritance?) I am fairly open to trying either one, but I am unsure of which is the best way to go
If you want different authorizations (for example Admin & User) level you can use Pundit.
If you have different roles with the same authorizations (like Student and Teacher in the same context) you should use Inheritance and Concerns, maybe defining a base controller from which all other controllers can derive.
Related
I'm fairly new to rails. I'm having problem on designing the model classes. So this app will be used by 5 different users(Students, Teachers, Head and Coordinator). They each are different users to login into the website and have different functionality (example: Head makes an event. Students register for an event. Coordinator sets who can be head etc). I have created all four models with USERNAME and PASSWORD on each models.I don't have user model right now because the users in this app are these 4 models. Now, while making login page, i'm having hard time on implementing the best way to authenticate the users. For example, If a Head puts its login credentials, the app should identify that user that logged in is Head. What approach will be best to encounter this?
Also, after not figuring out the way to approach this. I was thinking of using devise and CanCanCan gem. But the same promblem comes in even if i use this gems.(i maybe wrong)
Do not create multiple models for different kinds of users. This is almost always not what you want. Instead add a column called role of the type enum which contains all of the kinds of roles you want to add like Sergio pointed out. Your comment about having too many attributes on one model is a non issue compared to the one you are planning to create with 5 user models.
It sounds like you are possible putting too much data on the user model if that is your concern
and have different functionality (example: Head makes an event. Students register for an event.
For this you want a permissions system such as cancancan where you can specify which features of the website each role has access to.
I'm doing a Rails project with three user types: students, teachers, and administrators. Each user type has a dozen+ columns unique to that role. Initially I thought I'd create separate models for each type, but having a single shared login seems to pose a problem (I've found a workaround on Stack Overflow, but its complex and a few years old).
What is SOP for situations like this? Is it kosher to have a single user model with 24+ columns that will always be empty depending on the role type? Or am I better off sticking with three separate models and trying to hack a workaround to make a shared login?
Thanks!
EDIT: Oops, forgot to add the third workaround which I'm favoring: having a single user model with only columns relevant to login, and then models for each role that hold columns specific to each user type. Is that a good call?
You could make two separate models, Student and Teacher. Then add an admin:boolean field for teacher. I am assuming most admins will probably be teachers? Even if that is not the case you could just default that all admins are teachers. Three separate models is terribly bulky.
I'm making an app with 3 kinds of users: Teachers, Students and Admins.
Teachers can post Classes, with class info and assignments and stuff, and Students can enroll in those classes and do the assignments, get the materials and stuff. The Admins are just the people involved in running the site and will not be taking any of those actions, but just moderating and keeping an eye on things.
My question is, how do I built the separate authentication for those three cases? The users won't have anything in common and I'm gonna need 3 different logins/sign ups from the get-go, so it's not just a matter of assigning different roles.
How should I go about this?
All of those users have one important thing in common - they are all users. I don't see a reason for separating them. If you need users to sign up as a Teacher or Student, you can simply set up different views for those options and the default role that will be assigned to that user.
I don't see a reason why you think that it should be something different than assigning roles. If you really see many differences in data that will be stored for those different types of users, you can for example have have a common Users table and subclass them and store type-specific data in separate tables with reference to the User. However, for now I don't see anything in your description that can force something more than a simple role assigning.
I have two tables in my app's database for two kinds of users, therapists and patients, both sign in/out with a username and password, I want to make sure that there can't be a therapist and patient with the same username, how can I do that?
also, is it possible to use the same sessions controller for both therapists and patients?
Refactor Your Design
You shouldn't have two different tables for users. You should probably have a single User table, with a field that designates the role of the User. This makes it particularly easy to select the user's role using radio buttons or a drop-down in the web application, and also allows you to enforce uniqueness on aspects of a User record.
However, why is it impossible for a patient named "John Smith" to have a therapist named "John Smith?" I'd think about that before enforcing that particular uniqueness constraint.
Finally, a session is generally shared across controllers, and a controller can certainly read from more than one model at a time. In your case, refactoring to a common User model will reduce the need to do something complex in this regard.
I'm using Mongoid, Devise and Rails 3.1.
I have four models: Students, Teacher, Parents and School (the main account). All them will log in on system. But, I don't want create four ways to login. I want create an unique login method using anyone this models, but with respectives roles (This is the minor problem, I already can do that with CanCan).
Anybody have a easy solution, without create a programming-hell?
Actually, people logging on to your system are all Users. So either you choose to let the classesTeacher, Student, Parent, SchoolRepresentative to inherit from User using STI.
Most of the times I prefer simply that a User has roles. And the role would then be teacher, student ...
The roles define what a user is allowed to see.
Hope this helps.