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'