How to customize Devise to store more informations

2019-07-22 06:33发布

问题:

It seems that when Devise logs a user in, only the user id is stored in the session. This means that whenever calling current_user will result in a SQL query (something like User.find(:id)) to create the User object, which in many cases is unnecessary load.
For example, we may want to display the user's name in the top-right header, which is in the layout file and gets rendered in every response. For that we may just write current_user.name, which causes a SQL query to the users table.

I really want to store some basic informations(such as the name and email) of the current user in the session, to avoid that extra SQL query.

What is the right solution to this problem?

回答1:

Get over it already. That SQL query is effectively free. If you are really seeing performance issues from User.find(id), then your app/database/server are completely screwed.

Furthermore, by storing more data in the session, you open up the possibility of displaying data that is no longer current. Do you allow the user to edit their info - if so, what happens when they edit their name? It is hassle that has no real payoff.