3

in my rails 3 app, I want to define a variable like so:

PERSONAL_DOMAINS = %w[
  @126.com
  @163.com
  @aim.com
  @aol.com
]

I then want to be able to use this in user.rb for validations like so:

return true if PERSONAL_DOMAINS.any? { |rule| "@#{domain}".include? rule }

I also want to use it in another model.

Like so:

domain_rules = [PERSONAL_DOMAINS]
domain_rules.each { |rule| return false if !domain.match(rule).nil? }

With Rails, where and how should you define this list of PERSONAL_DOMAINS? YML, config, initializer? And then how do you use it correctly?

Thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • Possible duplicate of http://stackoverflow.com/questions/3598785/where-to-put-global-variables-in-rails-3 – Gardner Bickford Mar 25 '13 at 20:53
  • This is probably something you need to put in a YAML file and use an initializer to load it. – Artem Kalinchuk Mar 25 '13 at 20:57
  • how would you define this type of variable in a YAML? Also any performance issues? – AnApprentice Mar 25 '13 at 20:58
  • @AnApprentice Performance shouldn't be an issue since it only loads on application start. For loading YAML files, check this question: http://stackoverflow.com/questions/592554/best-way-to-create-custom-config-options-for-my-rails-app – Artem Kalinchuk Mar 25 '13 at 21:03

3 Answers3

3

There are 2 ways I'd consider doing this assuming the values don't change in different environments, in both cases I think you should put it in an initializer like:

config/initializers/personal_ip_addresses.rb

Then you could simply have a file with the constant:

  PERSONAL_DOMAINS = %w[
      @126.com
      @163.com
      @aim.com
      @aol.com
    ]

You'd use it simply as you have above:

return true if PERSONAL_DOMAINS.any? { |rule| "@#{domain}".include? rule }

OR it you wanted to go the config route:

YourApplicationClass::Application.config.personal_domains = %w[
      @126.com
      @163.com
      @aim.com
      @aol.com
    ]

and you'd use it like this:

Rails.configuration.personal_domains

personally I try to keep it simple and would go with the constant.

rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • The "config route" meaning defining your variable like config.my_variable = 10 inside the class block of your application.rb file – Jeffrey Kozik Oct 14 '22 at 01:43
1

You can define this in application.rb if you'd like it available to the whole app.

For example if you define something under

module MyApp
  class Application < Rails::Application

    PERSONAL_DOMAINS = %w[
      @126.com
      @163.com
      @aim.com
      @aol.com
    ]
    etc.

Then you can call it later as MyApp::Application::PERSONAL_DOMAINS

If that's what you really want. However, it's probably best to set up an initializer file to do this and keep it in that one place, easily changeable.

Richard Jordan
  • 8,066
  • 3
  • 39
  • 45
1

Have you tried configatron?

I generally use it for all configuration variables, and it works great!

tackleberry
  • 1,003
  • 2
  • 12
  • 16