title: Does rails have an opposite of 'humanize' for strings?
body: <p>Rails adds a <code>humanize()</code> method for strings that works as follows (from the Rails RDoc):</p>
  
  <pre><code>"employee_salary".humanize # =&gt; "Employee salary"
  "author_id".humanize       # =&gt; "Author"
  </code></pre>
  
  <p>I want to go the other way.  I have "pretty" input from a user that I want to 'de-humanize' for writing to a model's attribute:</p>
  
  <pre><code>"Employee salary"       # =&gt; employee_salary
  "Some Title: Sub-title" # =&gt; some_title_sub_title
  </code></pre>
  
  <p>Does rails include any help for this?</p>
  
  <h2>Update</h2>
  
  <p>In the meantime, I added the following to app/controllers/application_controller.rb:</p>
  
  <pre><code>class String
    def dehumanize
      self.downcase.squish.gsub( /\s/, '_' )
    end
  end
  </code></pre>
  
  <p>Is there a better place to put it?</p>
  
  <h2>Solution</h2>
  
  <p>Thanks, <a href="https://stackoverflow.com/users/13956/fd">fd</a>, for the <a href="http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html" rel="noreferrer">link</a>.  I've implemented the solution recommended there.  In my config/initializers/infections.rb, I added the following at the end:</p>
  
  <pre><code>module ActiveSupport::Inflector
    # does the opposite of humanize ... mostly.
    # Basically does a space-substituting .underscore
    def dehumanize(the_string)
      result = the_string.to_s.dup
      result.downcase.gsub(/ +/,'_')
    end
  end
  
  class String
    def dehumanize
      ActiveSupport::Inflector.dehumanize(self)
    end
  end
  </code></pre>