Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override rails naming conventions?

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)?

like image 223
chrishomer Avatar asked Jul 26 '09 17:07

chrishomer


People also ask

What is the naming convention in Rails?

Naming conventions in Active Record model Rails is capable of pluralizing (and singularizing) both regular and irregular words. Model class names must use the CamelCase form when composed of two or more words, while the database table names must use the snake_case form.

Should controllers be plural Rails?

Controller names are either singular or plural : Using “rails g resource” names the controllers plural, which makes sense because I think about them controlling many routes. Resource names are singular : They create a lot of mvc framework, the name you pass will become the model name, and let rails pluralize the rest.

Is naming convention important to avoid errors?

Using descriptive, consistent naming conventions is important and should not go overlooked. Variability and inconsistencies often lead to confusion, error and loss of time.

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.


2 Answers

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 
like image 94
Rich Seller Avatar answered Sep 18 '22 06:09

Rich Seller


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 
like image 32
chrishomer Avatar answered Sep 18 '22 06:09

chrishomer