问题
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.
回答1:
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...
回答2:
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.
来源:https://stackoverflow.com/questions/12039936/rails-one-model-for-authentication-and-profile-information-or-2-separate-models