如何自定义Devise以在会话中存储更多信息? [英] How to customize Devise to store more informations in the session?

查看:53
本文介绍了如何自定义Devise以在会话中存储更多信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎Devise登录用户时,会话中仅存储用户ID。
这意味着每次调用 current_user 都会导致SQL查询(例如 User.find(:id))创建User对象,在许多情况下,这是不必要的负载。

例如,我们可能希望在右上角标题中显示用户名,该标题位于布局文件中并获取在每个响应中呈现。为此,我们可以只写 current_user.name ,这会导致对用户表的SQL查询。

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.

我真的希望在会话中存储当前用户的一些基本信息(例如姓名和电子邮件),以避免不必要的SQL查询。

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?

推荐答案

已经克服了。该SQL查询实际上是免费的。如果您确实从 User.find(id)看到性能问题,那么您的应用程序/数据库/服务器就完全被搞砸了。

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.

这篇关于如何自定义Devise以在会话中存储更多信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆