Add user information in additional or current model? - ruby-on-rails

I currently have a "user model", which is managed by the gem devise. Right now, that model has only the most basic information (first name, last name, email, password).
Now I am wondering wether I should add additional user fields as an additional, associated model (e.g. named "userprofile") or just add the additional fields to the existing "user" model (profile picture, years of experience, description about the teacher etc.). What is the common practice?
I am looking forward your help.
Best regards,
Alex

When you call current_user (or any devise helper method per se), it retrieves all the fields from the database for that user record, whether or not you need 'em. So, it's better to have as lesser fields in the devise's user model as you can. If you want to create a user profile with additional fields, better create a new associated model.

In general, add those kinds of "simple" attributes directly to the User model (i.e. years of experience, description, etc). As you add more complicated information about a user (e.g. job history), or if you have a significant amount of these attributes such that it might start impacting performance, you may want to start adding associated models. But otherwise it's probably not worth the added complexity.

Related

How to set up Rails app that has different types of users?

If I want to build a Rails app that has two different types of users, let's say one type is called players and the other one is owners, what is the best and most efficient approach to modeling the app?
Things to take into account:
There should only be one Login, but different Registration forms that Owners/Players can use.
Owners can have access to a control panel but Players cannot.
Owners cannot share any of Players capabilities, but both need to be able to perform Login/Registration.
I am not using Devise, so please do not suggest it.
Different Approaches I've considered:
Using cancancan gem, but it does not really seem to meet my needs in the sense that I am not looking to create a user/admin hierarchical approach but rather a if you're a Player, then you can see these pages and perform these actions but Owners cannot and vice versa. Almost like splitting the app in two. cancancan seems that it would treat Owners as "Players with extra privileges", not different privileges entirely.
Creating separate models with separate login and registration forms, which seems like a disaster waiting to happen. One small mixup between a Players table and the Owners table, especially with the primary keys, and that will be a world of trouble where people could end up logging in to the wrong accounts.
Creating a polymorphic or has_one relation toward an Account model, which so far, seems like the best way to probably go about it. If I created a polymorphic Account model, I can store different types of Players/Owners, but how could I compare login credentials against all types?
I had been trying to find something on this matter regarding how to map this out and was surprised to not find an information on how to do this without using Devise. If anyone has any good links they can point me to that also address this matter (without Devise), please leave them in your answer! Thanks.
I'd suggest one User class with a type attribute that determines whether the user is a Player or an Owner (single table inheritance). This way you keep the registration logic in one place but can customize the forms depending on the user's class.
There must be alternatives to cancancan that help with what you want to do, or you can implement helpers yourself:
def can_access_control_panel?
current_user.is_a?(Owner)
end
You have to have a way to separate one user from another. One way is to add an attribute to the User table so you can call current_user.role and it will return "owner" or return "player".
I have used Pundit gem in the past. It lets you define which controller actions the current user is allowed to access. So as you create resources for your application, you can add a policy that specifies who is allowed to that given resource. This is the repo to the application.
This answer might help you.

Should I use Multiple user models or one big user model in Devise

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.

create a new model or use the user model?

In a rails application. If you have a User model and a post model and a user can post X number of times according to a plan.
Can I just modify the User object and add a plan_id to my user object or is it better to generate a new model called plan and associate it to a user?
It depends on what informations you want to store for the plan.
In my opinion, you should add a Plan model and add an association.
In that case, you can store all important informations about the plan itself in a Plan and use it to show them on the pricing page, as well.
But i guess, there is no "right" or "wrong" answer, just favorites. :)
There's no good way to answer this without knowing specifics or the future.
Might users end up having multiple plans?
How frequently are the plans required?
A "user" is not a "plan". A user has a plan. There should be a plan model; how it is stored is a separate issue. It may not be worth over-thinking at this point, either.

User profile. Should I create a new model?

this is basically a theoretical question.
What do you think is better for a normal rails app with users:
1)Create a Profile model where to put resume, images, links etc...
2)Put all the data in the user model.
The first choice maybe is cleaner but you have to load 2 models from the db, so maybe slower.
Thanks in advance.
I normally have a single User model. If I have unrelated resources that may deserve an associated model, than I create one for them.
For example, for me the Resume (assuming is not a single field) may deserve a dedicated Resume model, with a one-to-one association to User.
On the view, I normally create an /account resource that internally displays the account and provides the show, edit and update actions to view the account or update it.
The more models you have, the more your architecture will become complicated. So unless you have the need to split the fields out of the User model, I would keep them inside the model.
When you start to have several fields that may require a prefix, such as resume_title, resume_body, resume_created_at inside the User model, that's a good indication that you probably need a separate Resume model associated to the User.

rails: how to validate uniqueness of a field in two tables?

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.

Resources