(Rails) 使用主偏好设置回退管理用户偏好设置...?
我正在寻找一种促进用户偏好的机制。我还希望拥有一组“主”首选项,如果当前登录的用户没有设置特定的首选项,则可以使用这些首选项。我看到了几个与此类似的问题,但它们似乎进入了理论,而不是简单地提出了一个高质量的解决方案。
基本上,我正在寻找有关管理和存储的输入 - 模型、控制器等。最初,我考虑简单地使用包含 50 多个列的标准化表(用于性能等)。然而,我计划在未来添加各种未知的偏好,并且除了性能之外,我可以想象多个列会失控。想法?
I'm looking for a mechanism by which to facilitate user preferences. I also want to have a set of "master" prefs that are used if the currently logged in user doesn't have a specific pref set. I see several questions similar to this, but they seem to get into theory instead of simply proposing a quality solution.
Basically I'm looking for input on management as well as storage -- models, controllers, etc. Initially I was considering simply going with a normalized table of 50+ columns (for performance etc.). However, I plan on adding various, unknown preferences in the future and, performance aside, I could imagine multiple columns getting out of hand. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不需要根据数据库中的个人首选项进行操作或排序,那么您可能需要使用单个位掩码(整数)列。基本上,位掩码是一组表示为二进制数的开/关开关。例如,假设我们有三个首选项:
假设用户打开了 1 和 3,关闭了 2。使用 1 表示打开,0 表示关闭,其位掩码为:
101
(打开、关闭、打开)
这将作为
5
存储在数据库中,因为101
在二进制中是 5。位掩码很容易存储在数据库中(使用单个整数列),并且一旦您知道运算符(用于将用户的首选项合并到站点默认值中),就很容易操作。 Ryan Bates 有一个关于在 Rails 中使用位掩码的精彩教程:嵌入式关联。希望这能为您提供您正在寻找的具体示例。If you don't need to manipulate or sort by individual preferences in the database, then you might want to use a single bitmask (integer) column. Basically, a bitmask is a set of on/off switches represented as a binary number. For example, let's say we have three preferences:
Let's say a user has 1 and 3 on and 2 off. Using 1s for on and 0s for off, the bitmask for this is:
101
(on off on)
This gets stored in the database as
5
because101
is 5 in binary. Bitmasks are easy to store in the database (use a single integer column) and are easy to manipulate once you know the operators (for merging a user's preferences into the site defaults). Ryan Bates has a great tutorial on using bitmasks in Rails: Emmbedded Association. Hopefully that will give you the concrete example you're looking for.在我看来,定义和使用默认值的最佳方法是在用户首选项表中添加另一行,并将其作为类变量加载到模型中。如果未找到首选项,则覆盖访问器以查找默认值。类似这样:
本质上,默认首选项(从第 1 行加载)作为类变量存储在模型中。所有访问器都被重新定义并成为别名方法链的一部分,以便在返回值为 nil 时返回默认值。我想用||而不是 case,但如果用户将布尔首选项设置为 false,则会导致问题。
编辑:注意我不知道有什么好方法可以在不重新启动服务器的情况下更新 Rails 应用程序中的默认值。
In my mind, the best way to define and use defaults is to add another row in the user preferences table, and load it as a class variable in your model. Then override the accessors to find the defaults if the preference hasn't been found. Something like this:
Essentially the default preferences (loaded from row 1) are stored in the Model as a class variable. All the accessors are redefined and made part of an alias method chain so that the default would be returned if the returned value was nil. I wanted to use || instead of case, but that would cause problems in the event that the user had set a boolean preference to false.
Edit: N.B. I don't know of a good way to update the defaults in a rails app without restarting the server.