ActiveSupport Singularize Problem

April 9, 2010 • Damien White

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'

Posted in ruby on rails and tagged with Rails, Ruby

Damien White

I am a software architect with over 16 years of experience. I simply love coding! I have a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. I'm happy to share what I know in my quest to learn as much as possible. I focus most of my time on web development using Ruby on Rails, Ember.js, and ASP.NET MVC.

comments powered by Disqus