Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4260976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:59:33+00:00 2026-05-21T05:59:33+00:00

Rails adds a humanize() method for strings that works as follows (from the Rails

  • 0

Rails adds a humanize() method for strings that works as follows (from the Rails RDoc):

"employee_salary".humanize # => "Employee salary"
"author_id".humanize       # => "Author"

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:

"Employee salary"       # => employee_salary
"Some Title: Sub-title" # => some_title_sub_title

Does rails include any help for this?

Update

In the meantime, I added the following to app/controllers/application_controller.rb:

class String
  def dehumanize
    self.downcase.squish.gsub( /\s/, '_' )
  end
end

Is there a better place to put it?

Solution

Thanks, fd, for the link. I’ve implemented the solution recommended there. In my config/initializers/infections.rb, I added the following at the end:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:59:33+00:00Added an answer on May 21, 2026 at 5:59 am

    the string.parameterize.underscore will give you the same result

    "Employee salary".parameterize.underscore       # => employee_salary
    "Some Title: Sub-title".parameterize.underscore # => some_title_sub_title
    

    or you can also use which is slightly more succinct (thanks @danielricecodes).

    • Rails < 5 Employee salary".parameterize("_") # => employee_salary
    • Rails > 5 Employee salary".parameterize(separator: "_") # => employee_salary
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a rails gem that adds functionality to Rails' fields_for method, and I'd
I'm writing a mountable Rails 3.1 engine that adds features to the main app
I want to write a gem that adds a app/services directory to a Rails
I am writing my first Rails gem, which adds a method to ActiveRecord. I
I did rails generate controller home index But it adds this line to my
Rails make It possible to map resources that are logically children of other resources
Rails has a nice idiom that makes it easy for you to have a
I'm using the event_calendar gem in a rails app. Event_calendar adds the following to
I have a single Rails 2.2.2 app that I want to 'share' with multiple
I'm building a rails app that allows athletes to create and manage timesheets for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.