开发者

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.

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜