How to customize Devise to store more informations in the session?
It seems that when Devise logs a user in, only the user id i开发者_高级运维s 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.
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?
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.
精彩评论