title: Ruby on Rails 4 - Model to adjust Rails.configuration
body: <p><strong>UPDATED AGAIN</strong>
  I've modified the applicaiton to handle building a form for each setting entry.  The form posts to /Settings with a :put method and is looped in a transaction block.</p>
  
  <p>Here is my code:</p>
  
  <pre><code># settings_controller.rb
  class SettingsController &lt; ApplicationController
    def index
      @settings = Setting.all
    end
  
    def update
      Setting.transaction do
        params[:settings].each do |k, v|
          @setting = Setting.find(k)
          @setting.update(params.require(:settings)[k].permit(:value))
        end
      end
  end
  
  # settings/index.html.erb
  &lt;%= form_tag "/settings", :method =&gt; "put" do %&gt;
      &lt;table&gt;
        &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Setting&lt;/th&gt;
          &lt;th&gt;Value&lt;/th&gt;
        &lt;/tr&gt;
        &lt;tbody&gt;
      &lt;% @settings.each  do |setting| %&gt;
          &lt;tr&gt;
          &lt;%= fields_for "settings[]", setting do |sf| %&gt;
              &lt;td&gt;&lt;%= sf.label :name, t(setting.name) %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= sf.text_field :value %&gt;&lt;/td&gt;
          &lt;% end %&gt;
        &lt;/tr&gt;
      &lt;% end %&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;%= submit_tag("Save Changes") %&gt;
      &lt;%= link_to 'back', articles_path %&gt;
  &lt;% end %&gt;
  
  #models/article.rb
  class Article &lt; ActiveRecord::Base
    has_many :comments, dependent: :destroy
    validates :title, presence: true,
              length: { minimum: (Setting.find_by_name("validation_article_title_length_min").value.to_i) }
  end
  </code></pre>
  
  <p>I don't think this is a good way to do this and here is why:
  I had to jump through hoops to get the view to render each setting since it is part of multiple records.  I am using an i18n translation to translate the database name of "validation_article_title_length_min" to "Minimum Article Title Length"... I don't know if that is normal or not.</p>
  
  <p>I can't validate each record in the model this way specific to what each setting may require.</p>
  
  <p>And finally (and this is a big one) the article validation is not working. It will accept the value from the database and work as expected but upon changing the value via the form (and I verified the value did change), the validation sticks with the old number.  After some time passes it starts using thew new number.</p>
  
  <p>There has got to be an easier way to do all of this?  Perhaps I should modify the migration so that each setting is its own column?  Doing that would fix my form and the transaction loop I have to do in my controller.
  But at the end of the day the validation in article isn't changing with a database update and I do not know why.</p>
  
  <p>Thanks again for guidance on this.</p>
  
  <p><strong>UPDATED</strong>
  Thanks to comments by @Frederick Cheung, I believe it may be best to go with a database entry for settings.  Doing this poses two problems:
  I want the settings index page to show all the settings and allow editing and posting from that page.</p>
  
  <p>If I were to have 5 validation settings, clicking post should ensure all 5 were valid before posting a single one to the database.</p>
  
  <p>Also, how would you recommend storing this in the database?  I've played around with the migration and I can either make each column a different application setting.  Or I can make each setting a row.</p>
  
  <p>Thanks again for the help; I am hoping this all clicks soon.</p>
  
  <p><strong>Previous Post:</strong></p>
  
  <p>I have a brand new rails project with the "getting started" tutorial from the rails site.  Everything is working as expected.</p>
  
  <p><strong>My Task:</strong>
  Currently users can add an article.  The article title must be > 5 characters.  I wanted to make this number an environment configuration variable.  Then, I wanted to be able to adjust this variable from the web page.</p>
  
  <p><strong>What I've Done so far</strong>
  I created a settings resource.  I tried saving settings to the database as individual rows but this meant each setting could be edited only one at a time.  After googling I found that configuration variables could be created in <code>config/article_valdiation.yml</code></p>
  
  <pre><code>default: &amp;default
    title_length_min: 5
  
  development:
    &lt;&lt;: *default
    bar: dev
  
  test:
    &lt;&lt;: *default
    bar: test
  </code></pre>
  
  <p>Then in the <code>config/environment.rb</code> I did the following:</p>
  
  <pre><code>Rails.configuration.article = ActiveSupport::OrderedOptions.new()
  Rails.configuration.article.validation = Rails.application.config_for(:article_validation).deep_symbolize_keys
  </code></pre>
  
  <p>I am trying to create a structure like Rails.configuration.element.configcatagory[config_element]</p>
  
  <p>Here is the settings route and view. </p>
  
  <pre><code># routes.rb
  resources :settings do
      collection do
        put :update
        patch :update
      end
    end
  
  # settings/index.html.erb
  &lt;% render plain: params[:setting].inspect %&gt;
  &lt;h1&gt;Application Settings&lt;/h1&gt;
  &lt;%= form_tag("/settings", :method =&gt; "put") do %&gt;
      &lt;table&gt;
        &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Setting&lt;/th&gt;
          &lt;th&gt;Value&lt;/th&gt;
        &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td&gt;Minimum Article Title Length&lt;/td&gt;
            &lt;td&gt;&lt;%= text_field_tag('article[:title_length_min]', Rails.configuration.article.validation[:title_length_min])  %&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;%= submit_tag("Save Changes") %&gt;
      &lt;%= link_to 'back', articles_path %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>The Problem</strong>
  Perhaps I am configuring things incorrectly but...:</p>
  
  <ol>
  <li>I can't determine how to setup the view so the params is sent to specify both "article", "validation" and "title_length_min".</li>
  <li>I don't know how to make the control parse the params properly.  It gets the params but I don't know what to do with them since I can't do the usual Setting.find and Setting.update(params.require(:something, :somethingelse)</li>
  <li>I feel like there should be a model to configure these custom configurations but I am at a loss on where to start to make that work.</li>
  </ol>