Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: One model for Authentication and Profile information or 2 separate models

In many tutorials (especially for authentication), speakers say to put user authentication and profile information in the same table (model) called User.

My question is simple: Is it safe to put everything in one table? (bonus: is this the best practice?)

I would rather suggest to separate authentication information (email, password, salt,...) and profile information (first name, last name, birth day, location, gender,...) in two models: User (for authentication) and Profile, and linking models by has_one/belongs_to associations.

Am I wrong? What do you suggest me?

Thanks.

like image 862
htaidirt Avatar asked Aug 20 '12 14:08

htaidirt


2 Answers

If you want to follow database normalisations you should separate the tables. However, sometimes it is not the best option... For example, if your table users has just email, password (for authentication) and name. I won't create a profile table just to store the name, right?

So, it will depend on your requirements to make your design decisions....

I found this interesting post about it, where @D Roddis explain some advantages and disadvantages about three different approaches: Storing User Profile in Users Table, Storing User Profile in User_Profile Table 1-1 relationship to users and Storing User Profile as properties and values in tables.

I hope it helps...

like image 53
gabrielhilal Avatar answered Nov 15 '22 03:11

gabrielhilal


I'd put them in seperate models myself.

How many actions are there which operate on user and profile together? Not many, There are a lot in both constructs where they need to know nothing (or nothing more than the id) about each other.

like image 34
Tony Hopkinson Avatar answered Nov 15 '22 05:11

Tony Hopkinson