我应该在哪里存储常量数据,然后如何在Rails中引用它?

时间:2021-05-29 23:06:49

I have a array of data, for example times (7:00am, 7:30am, etc.), that I want stored and referenced in a couple places.

我有一系列数据,例如时间(早上7点,上午7点30分等),我希望在几个地方存储和引用。

1) Where should I store this data? I was originally thinking in my DB (I'm using mongoid) but I'm not sure if that's over kill.

1)我应该在哪里存储这些数据?我最初在我的数据库中思考(我正在使用mongoid),但我不确定这是否过度杀戮。

2) How would I go about referencing it? Let's say, from a drop down menu.

2)我将如何引用它?让我们说,从下拉菜单中。

5 个解决方案

#1


5  

In this kind of situation, I create a Constants module in lib:

在这种情况下,我在lib中创建一个Constants模块:

module Constants
  SCHEDULER_STEPS = %w( 7:00am 7:30am )
end

Then I access it wherever I need with:

然后我随时随地访问它:

Constants::SCHEDULER_STEPS

Note: be sure to add libs to your autoload path in the configuration file.

注意:确保将libs添加到配置文件中的自动加载路径。

#2


2  

I prefer to put this sort of data on the model it's most closely related to. For example, if the times in your example were the times to run a backup, put them in the Backup model with the rest of the behavior related to backups:

我更喜欢把这类数据放在与它最密切相关的模型上。例如,如果示例中的时间是运行备份的时间,请将它们放在备份模型中,其余的行为与备份相关:

# app/models/backup.rb
class Backup < ActiveRecord::Base
  AVAILABLE_RUN_TIMES = %w{7:00am 7:30am ...}

  def run_at=(date)
    BackupSchedule.create(self, date)
  end
end

# app/views/backups/_form.html.erb
<%= select_tag(:run_at, options_for_select(Backup::AVAILABLE_RUN_TIMES)) %>

I've used the "big bucket of constants" approach too, but I would only use it if there's truly no more relevant place for the constants to live.

我也使用了“常量大桶”的方法,但我只会使用它,如果真的没有更多相关的常量生存的地方。

#3


2  

For such kind of requirements i prefer

对于这种要求,我更喜欢

1st) create a config/app_constants.yml

1)创建一个config / app_constants.yml

Code here

production:
  time_list: "'7:00am','7:30am','7:40am'"
test:
  time_list: "'7:00am','7:30am','7:40am'"
development:
  time_list: "'7:00am','7:30am','7:40am'"

2nd Create under lib/app_constant.rb

在lib / app_constant.rb下创建第二个

module AppConstant
  extend self

  CONFIG_FILE = File.expand_path('../config/app_constants.yml', __FILE__)
  @@app_constants = YAML.load(File.read(CONFIG_FILE))
  @@constants = @@app_constants[Rails.env]

  def get_time_list
    @@constants['time_list'].split(',')
  end
end

3rd Call it anywhere like

3在任何地方打电话给它

AppConstant.get_time_list #will return an array

With this you just have to make changes at a single clean place(app_constants.yml) and will reflect throughout you application wherever AppConstant.get_time_list is used

有了这个,您只需在一个干净的地方(app_constants.yml)进行更改,并且无论在哪里使用AppConstant.get_time_list,都会反映在整个应用程序中

#4


2  

There is a rails cast on this exact issue. You can view it here:

在这个问题上有一个轨道。你可以在这里查看:

http://railscasts.com/episodes/85-yaml-configuration-file

#5


1  

I ended up creating a "global_constants.rb" file in "/config/initializers" with the following code:

我最终使用以下代码在“/ config / initializers”中创建了一个“global_constants.rb”文件:

module Constants
    BUSINESS_HOURS = ["6:00am","6:15am","6:30am","6:45am","7:00am"]
end

Then I called the data with Constants::BUSINESS_HOURS, specifically for the select box, the code was: <%= f.input :hrs_op_sun_open, :collection => Constants::BUSINESS_HOURS %>

然后我使用Constants :: BUSINESS_HOURS调用数据,专门用于选择框,代码为:<%= f.input:hrs_op_sun_open,:collection => Constants :: BUSINESS_HOURS%>

Many of the answers here seem viable and I suspect they are all correct ways of doing what I needed.

这里的许多答案似乎都是可行的,我怀疑它们都是正确的方法来做我需要的。

#1


5  

In this kind of situation, I create a Constants module in lib:

在这种情况下,我在lib中创建一个Constants模块:

module Constants
  SCHEDULER_STEPS = %w( 7:00am 7:30am )
end

Then I access it wherever I need with:

然后我随时随地访问它:

Constants::SCHEDULER_STEPS

Note: be sure to add libs to your autoload path in the configuration file.

注意:确保将libs添加到配置文件中的自动加载路径。

#2


2  

I prefer to put this sort of data on the model it's most closely related to. For example, if the times in your example were the times to run a backup, put them in the Backup model with the rest of the behavior related to backups:

我更喜欢把这类数据放在与它最密切相关的模型上。例如,如果示例中的时间是运行备份的时间,请将它们放在备份模型中,其余的行为与备份相关:

# app/models/backup.rb
class Backup < ActiveRecord::Base
  AVAILABLE_RUN_TIMES = %w{7:00am 7:30am ...}

  def run_at=(date)
    BackupSchedule.create(self, date)
  end
end

# app/views/backups/_form.html.erb
<%= select_tag(:run_at, options_for_select(Backup::AVAILABLE_RUN_TIMES)) %>

I've used the "big bucket of constants" approach too, but I would only use it if there's truly no more relevant place for the constants to live.

我也使用了“常量大桶”的方法,但我只会使用它,如果真的没有更多相关的常量生存的地方。

#3


2  

For such kind of requirements i prefer

对于这种要求,我更喜欢

1st) create a config/app_constants.yml

1)创建一个config / app_constants.yml

Code here

production:
  time_list: "'7:00am','7:30am','7:40am'"
test:
  time_list: "'7:00am','7:30am','7:40am'"
development:
  time_list: "'7:00am','7:30am','7:40am'"

2nd Create under lib/app_constant.rb

在lib / app_constant.rb下创建第二个

module AppConstant
  extend self

  CONFIG_FILE = File.expand_path('../config/app_constants.yml', __FILE__)
  @@app_constants = YAML.load(File.read(CONFIG_FILE))
  @@constants = @@app_constants[Rails.env]

  def get_time_list
    @@constants['time_list'].split(',')
  end
end

3rd Call it anywhere like

3在任何地方打电话给它

AppConstant.get_time_list #will return an array

With this you just have to make changes at a single clean place(app_constants.yml) and will reflect throughout you application wherever AppConstant.get_time_list is used

有了这个,您只需在一个干净的地方(app_constants.yml)进行更改,并且无论在哪里使用AppConstant.get_time_list,都会反映在整个应用程序中

#4


2  

There is a rails cast on this exact issue. You can view it here:

在这个问题上有一个轨道。你可以在这里查看:

http://railscasts.com/episodes/85-yaml-configuration-file

#5


1  

I ended up creating a "global_constants.rb" file in "/config/initializers" with the following code:

我最终使用以下代码在“/ config / initializers”中创建了一个“global_constants.rb”文件:

module Constants
    BUSINESS_HOURS = ["6:00am","6:15am","6:30am","6:45am","7:00am"]
end

Then I called the data with Constants::BUSINESS_HOURS, specifically for the select box, the code was: <%= f.input :hrs_op_sun_open, :collection => Constants::BUSINESS_HOURS %>

然后我使用Constants :: BUSINESS_HOURS调用数据,专门用于选择框,代码为:<%= f.input:hrs_op_sun_open,:collection => Constants :: BUSINESS_HOURS%>

Many of the answers here seem viable and I suspect they are all correct ways of doing what I needed.

这里的许多答案似乎都是可行的,我怀疑它们都是正确的方法来做我需要的。