Where is the best place to store application parameters : database, file, code…?

后端 未结 5 1624
礼貌的吻别
礼貌的吻别 2021-01-03 10:24

I am developing a Ruby on Rails website and I have an \"architectural\" question : my application needs some parameters and I\'m wondering where to store them.

In co

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 10:52

    I put them in the database. I have a lot of these, and they are all pretty straightforward lists of strings. The tables are all the same - id, name, description.

    I generate models for them rather than having an actual model file for each one. In app/models I have a file called active_record_enums.rb, which in your case would look something like this:

    ACTIVE_RECORD_ENUMS = %w{
      ValidationStatus
      SendingStatus
    }
    
    ACTIVE_RECORD_ENUMS.each do |classname|
      eval "class #{classname} < ActiveRecord::Base; end"
      classname.constantsize.class_eval do 
        # Add useful methods - id_for(name) and value_for(id) are handy
      end
    end
    

    This file has to be required in a config file somewhere; other than that it's pretty straightforward.

提交回复
热议问题