59

For example, I'm using "Bonus" as my model, so I'd expect "bonuses" to be the plural form and "bonus" to be the singular form.

However, in Ruby, this results in:

"bonus".pluralize # bonus
"bonuses".singularize # bonuse

So, when I do a "has_many :bonuses", for example, it doesn't use the Bonus.rb model (since Ruby expects a Bonuse.rb model instead). Is there a way to correct that in Ruby on Rails somehow such that "bonuses" acts as the plural form for the model bonus.rb?

sjsc
  • 4,552
  • 10
  • 39
  • 55
  • 1
    Similar question: http://stackoverflow.com/questions/1185035/how-do-i-override-rails-naming-conventions – Chubas Aug 19 '10 at 01:17

3 Answers3

110

In config/initializers, you will find a file called inflections.rb. There are some instructions in here, but you will want something along the lines of:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'bonus', 'bonuses'
end
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
3

Just to back up bcarlso, more on Inflector can be found here:

http://4loc.wordpress.com/2009/04/09/inflector-rails-pluralization/

Note that the position of the Inflector.inflections block is important and, as noted in the link reference, must be after the Initializer.run block.

Warren
  • 535
  • 6
  • 12
1

I believe you use the Inflector in your environment.rb (memory's a bit sketchy though) If I remember correctly you put it in a block

Inflector.inflections { | i | i.irregular 'bonus', 'bonuses' }
bcarlso
  • 2,345
  • 12
  • 12