Coding Tidbits

Cube Root is an exclusive Ruby on Rails Consultancy. Find our programming ramblings here.

Rails ActiveSupport Inflections

We know, In Rails the Inflector transforms words from singular to plural, class names to table names .

The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb .

In this post, we will go through one example explaining the inflections .

Consider, leaves_controller with Leave model . For this controller routes generated are as follows -

new_leafe   GET    /leaves/new(.:format)      leaves#new
edit_leafe  GET    /leaves/:id/edit(.:format) leaves#edit
leafe       GET    /leaves/:id(.:format)      leaves#show

> 'leaves'.singularize
=> "leafe"

here, the singularize format of ‘leaves’ is taken as ‘leafe’ .

Suppose, we want to change the singularize format to ‘leaf’, then -

Add rule in config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'leaf', 'leaves'

Now, check routes for same -

new_leaf   GET    /leaves/new(.:format)        leaves#new
edit_leaf  GET    /leaves/:id/edit(.:format)   leaves#edit
leaf       GET    /leaves/:id(.:format)        leaves#show
> 'leaves'.singularize
=> "leaf"

It Works! In this way, by adding inflection rules, we can override naming conventions in rails .