Blog - .NET, ASP.NET, AJAX, Ruby and more
  • Google Banner 2

ActiveSupport Singularize Problem

I just happened to encounter a problem with the ActiveSupport inflector in my Rails app.  Here’s the problem: words that end in ess seem to be an issue.  For example:

'dress'.singularize # => 'dres'
'business'.singularize # => 'busines'
'address'.singularize # => 'addres'

D’oh, where did that ending ‘s’ go?

Turns out that “Rails has a longstanding policy of not taking further inflector patches. Use an initializer in your application instead,” a quote from Mike Gunderloy.   What exactly does this mean?  Simple…

Within your Rails application, open the config/initializers/inflections.rb file and just add the following:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.singular(/ess$/i, 'ess')
end

All better…

'dress'.singularize # => 'dress'
'business'.singularize # => 'business'
'address'.singularize # => 'address'
This entry was posted in Ruby on Rails and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.

Damien White is a developer in Connecticut that simply loves coding. Ever since he was young, he has had a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. He’s happy to share what he knows in his quest to learn as much as possible.