How do I override rails naming conventions?

Ruby on-RailsNaming ConventionsPluralize

Ruby on-Rails Problem Overview


I have a model named "clothing" which I want to be the singlular (one piece of clothing). By default, rails says the plural is clothings. Right or wrong, I think it will be more readable if the plural is "clothes".

How do I override the plural naming convention? Can I do it right in the model so I don't have to do it over and over? How will this change how routes are handled (I am using restful architecture)?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I'm no RoR expert, but did find a possible approach. From the referenced site you can add inflection rule inside the config/initializers/inflections.rb file:

# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'clothing', 'clothes'
end

Solution 2 - Ruby on-Rails

For rails 2.3.2 and maybe 2+, you need to do it a little different:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.plural /^(ox)$/i, '\1\2en'
    inflect.singular /^(ox)en/i, '\1'

    inflect.irregular 'octopus', 'octopi'

    inflect.uncountable "equipment"
end

Solution 3 - Ruby on-Rails

Add this in your environment.rb file if you are trying to stop database pluralization

ActiveRecord::Base.pluralize_table_names = false

Solution 4 - Ruby on-Rails

With Ruby 2.2.2 windows or linux for me best solve was :

ActiveRecord::Base.pluralize_table_names = false

class Persona < ActiveRecord::Base
end


personas = Persona.all
personas.each do | personita |
  print "#{personita.idpersona}   #{personita.nombre}\n"
end

p Persona.count

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchrishomerView Question on Stackoverflow
Solution 1 - Ruby on-RailsRich SellerView Answer on Stackoverflow
Solution 2 - Ruby on-RailschrishomerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsShan ValleruView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJAGJ jdfoxitoView Answer on Stackoverflow