在Rails应用程序中存储全局应用程序设置的最佳方法是什么? [英] What's the best way to store global application settings in a Rails application?

查看:103
本文介绍了在Rails应用程序中存储全局应用程序设置的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要处理两种全局配置设置:

I want to handle two kinds of global configuration settings:


  • 用户可以更改的设置,如通知邮件

  • 与特定产品版本相关联的设置,例如禁用免费版本中的功能(仅在商业版本中可用)。

储存这些设定的最佳方法是什么?数据库,配置文件,硬编码在源代码中,...?

What's the best way to store these settings? Database, configuration file, hardcoded in the source, ...?

推荐答案

你将对多个人/产品使用相同的结构,这是有道理的。

For both cases database. You're going to be using the same structures for multiple people/products so it makes sense. Also it allows you to change things without restarting the server.

过去我已经这样处理了:
对于特定于用户的设置,我创建了一个UserSettings模型/表,它与用户有一对一的关系。这是因为我的大多数涉及用户的操作不需要加载这些设置,所以当我需要时,它们只包含在数据库的用户加载中。

I've handled it this way in the past: For settings specific to the user, I've created a UserSettings model/table, that has a one-to-one to relationship with a user. The reasoning for this is that the majority of my operations involving users do no not require these settings to be loaded, so they're only included on user loads from the database when I need them.

当我这样做,我通常将我的列名称,以便我可以写帮助程序,动态创建基于名称。这意味着我不必修改我的意见,以纳入新的设置,除非我添加一个不同的命名方案。

When I do this, I'll usually group my column names, so that I can write helpers that dynamically create based on the names. Meaning that I won't have to modify my views to incorporate new settings unless I add one with a different naming scheme.

对于产品的特定设置,取决于你如何做事情。有几种方法可以解释你的问题。

For the settings specific to a product, well that depends on how you are doing things. And there are a couple of ways to interpret your question.

我读的方式是你想决定一个产品的水平。用户可以覆盖或禁用用户设置的设置。并可能定义一些产品特定的设置。

The way I read it is that you want to decide on a product level. What settings users can overriding or disabling a user's setting. And possibly define some product specific settings.

我将使用一对多产品来设置关系。设置表将是一个简单的东西(product_id,setting_name,setting_default_value,allow_user_change)

I would use a one-to-many product to setting relationship. The setting table would be something simplistic (product_id, setting_name, setting_default_value, allow_user_change)

这做了很多事情。它允许您有不同产品的设置的可变列表(适用于您提供许多不同的产品,而不是不同的服务访问层次的情况下)。它还允许您定义用户可以/不能更改的设置,并为该产品类型指定值。可以从管理员视图更改,而无需重新启动应用程序。它也不绑定到用户设置,到一个点,如果用户没有在product_settings中列出的设置将没有问题。

This does a number of things. It lets you have a variable list of settings for different products (Perfect for the case where you're offering many different products instead of varying tiers of access to services). It also lets you define what settings a user can/can't change and give values for that product type. That can be changed from an administrator view without restarting the application. It's also not tied to user settings, to the point where if a user doesn't have a setting listed in the product_settings there will be no problems.

缺点是你将在此表中有多个常用设置。我会将每个产品的设置都移到产品表中的字段中。

The downside is you will have multiple common settings in this table. I would move settings that every product will have a different value to a field in the product table.

您还必须进行验证,以确保用户不会更改一个设置他们的产品说他们不能。您还必须编写帮助方法来合并产品和用户端的设置。

You will also have to write validations to ensure that a user does not change a setting their product says they can't. You will also have to write helper methods to merge settings from the product and user sides.

这篇关于在Rails应用程序中存储全局应用程序设置的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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