I need very accurate pluralization for my application
I am currently using the Rails built in pluralization method.
I found a problem with the word 'foot'.
When I do:
"foot".pluralize(2)
=> "foots"
The correct pluralization should be feet.
How can I get more accurate results?
I have looked for other gems and they all seem to make the same error.
you need to define below code inside your config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'foot', 'feet'
end
there are some of plural are not define yet, so you need to define it inside inflections.
Related
I have a model Leave and a controller name 'LeavesController'. When I set resources :leave[singular] to the routes.rb then the routes name are ok. But when I set the resources :leaves[plural], then I get some funny routing name. See screenshot for details. [leafe, new_leafe, edit_leafe]
How can I get rid of these funny routing name?
In config/initializers/inflections.rb uncomment/add the following
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'leave', 'leaves'
end
Explanation:
Rails uses Inflectors to singularize/pluralize words automatically. This is how things like singular model names are transformed into their plural table names. Rails is typically able to guess the correct pluralization/singularization but can sometimes get confused. The inflections.rb initializer allows you to explicitly define how you want to handle singularization/pluralization when Rails doesn't give you what you are looking for.
So. For some reason I'm struggling with Rails naming today. I feel like the best naming for a model I'm creating is DNSRecord for the camel case class model name and dns_record for snake case referencing -- Rails wants to name it DnsRecord.
I've seen a solution if the entire word is capitalized for example: API
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end
but doesn't seem to work well as:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'DNSRecord'
end
it still wants to refer to DnsRecord. I know this is a small thing but DNSRecord feels better to me.
I believe you will want inflect.acronym 'DNS' rather than 'DNSRecord'. You will likely also have to restart your server after updating the inflections file.
Ruby on rails uses singular and plural conventions for model, view and controller names. This is very good, since one might have a model called user and a controller called users. This work with almost every word, such as user(s), tree(s), book(s), so every word that just has an s to form the plural.
However, what about words that do not just need an s to form the plural, such as words ending with the letter "y"? E.g., city and cities? Does rails know the difference or would I need to write city and citys, even though it is grammatically wrong?
Rails knows a lot of plurals. It can handle "city," for example:
1.9.2p318 :001 > "city".pluralize
=> "cities"
1.9.2p318 :002 > "cities".singular
=> "city"
However, you may find plurals it doesn't know, and won't be learning. See the documentation for ActiveSupport::Inflector
The Rails core team has stated patches for the inflections library
will not be accepted in order to avoid breaking legacy applications
which may be relying on errant inflections. If you discover an
incorrect inflection and require it for your application, you’ll need
to correct it yourself (explained below).
How do you correct it yourself? In config/initializers/inflections.rb. For example:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural /^(.*)(l)ens$/i, '\1\2enses'
end
See again the docs for ActiveSupport::Inflector for more on how to teach rails new inflections.
For a lot of common ones, it knows how to handle it pretty well. You can try this yourself in IRB by doing the following:
require 'active_support/all'
ActiveSupport::Inflector.pluralize("city")
And you'll get back a string with the value "cities". You can also add and adjust inflections by following the steps listed in config/initializers/inflections.rb
I am defining some active records with acronyms. RvPark (Recreational Vehicle Park). When I titleize the class name, I get 'Rv Park'. It really should be 'RV Park'. Is there a good way to do this? Since this model shares code with other models, I need to create a generic solution, but I haven't been able to come up with one.
I did see a discussion on this, but there wasn't a solution that worked for me. any insight is appreciated.
https://rails.lighthouseapp.com/projects/8994/tickets/2944-titleize-doesnt-take-all-uppercase-words-into-account
You can do this by configuring ActiveSupport::Inflector, which provides the titleize method. Simply define your own inflections in an initializer.
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'RV'
end
Restart your app to pick up the change. Now titleize knows how to handle "RV". Fire up a Rails console to check it out:
> "RvPark".titleize
=> "RV Park"
> "rv".titleize
=> "RV"
See the linked docs for more cool stuff you can do with inflections.
UPDATE: Acronym support was added to the Rails Inflector after I posted this.
See #Anson's answer for Rails 3.2 and up.
This looks like an edge-case that titleize wasn't designed for. The problem is the capitalize call inside will always turn RV into Rv.
I would create a generic name function for the models that call self.class.titleize internally, and then overload it in the RVPark model.
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?
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
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.
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' }