Fun with the Rails Inflector
Ruby on Rails, Technology & Software November 11th, 2006Updated for Rails 2.0.x. I always hate it when I run into old content about Rails on the web that doesn’t work because its outdated. Fortunately a reader, John R, pinged me and reminded me that I was being “that guy”; so I updated this post to be accurate as of Rails 2.0.x. Thanks John!
For those of you who are trying to roll on Rails, the inflector doesn’t quite work as expected with namespaces. When I create a model for media, I add the following to the Inflector settings in config/initializers/inflections.rb.
Inflector.inflections do |inflect|
inflect.uncountable "media"
end
Then generate a scaffold for Admin::Media
C:/Projects/medias_app>ruby script/generate scaffold Admin::Media
create app/controllers/admin
create app/helpers/admin
exists app/views/admin/medias
create test/functional/admin dependency model
create app/models/
create test/unit/
create test/fixtures/
create app/models/medias.rb
create test/unit/medias_test.rb
create test/fixtures/medias.yml
identical app/views/admin/medias/_form.rhtml
create app/views/admin/medias/list.rhtml
create app/views/admin/medias/show.rhtml
create app/views/admin/medias/new.rhtml
create app/views/admin/medias/edit.rhtml
create app/controllers/admin/medias_controller.rb
create test/functional/admin/medias_controller_test.rb
create app/helpers/admin/medias_helper.rb
create app/views/layouts/medias.rhtml
create public/stylesheets/scaffold.css
Loaded suite script/generate
Started
Finished in 0.0 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
I get a medias model in the admin folder (close but no cigar! what is “medias” anyway?).
The fix? Add the following to the inflector in config/initializers/inflections.rb as a work around:
Inflector.inflections do |inflect|
inflect.uncountable "media", "admin::media"
end
Now generate the Admin::Media scaffold, controller, (whatever) and viola! Your generated code should function properly.
C:/Projects/media_app>ruby script/generate scaffold Admin::Media
create app/controllers/admin
create app/helpers/admin
exists app/views/admin/media
create test/functional/admin
dependency model
create app/models/
create test/unit/
create test/fixtures/
create app/models/media.rb
create test/unit/media_test.rb
create test/fixtures/media.yml
identical app/views/admin/media/_form.rhtml
create app/views/admin/media/list.rhtml
create app/views/admin/media/show.rhtml
create app/views/admin/media/new.rhtml
create app/views/admin/media/edit.rhtml
create app/controllers/admin/media_controller.rb
create test/functional/admin/media_controller_test.rb
create app/helpers/admin/media_helper.rb
create app/views/layouts/media.rhtml
create public/stylesheets/scaffold.css
Loaded suite script/generate
Started
Finished in 0.0 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
November 12th, 2006 at 12:02 am
Heard you’re busy working hard…good luck, hope it’s going well.
December 4th, 2006 at 6:10 pm
I found this post useful. I was having problems with a table called it_equipment. I used
Inflector.inflections do |inflect|
inflect.uncountable “equipment”
end
But had the same problem in that my model name was correct but controller and views wanted to be it_equipments. Generating the scaffold with
ruby script/generate scaffold ItEquipement ItEquipment
Thanks for the tip.
March 13th, 2007 at 10:52 am
Thanks for the post here:
http://wiki.rubyonrails.org/rails/pages/Gotcha
I just spent a good part of an hour punching code till I googled that page. Who though gsub could be such a pain!
September 21st, 2007 at 9:02 pm
Thanks for your post! Had problems with “tax”. Although rails pluralization is tax -> taxes, there is a mix up with taxes ~ taxis.
I got “uninitialized constant Taxis” error…
March 25th, 2008 at 2:24 pm
I’ve tried this to fix my troubles with a scaffold named ‘press’. Even after adding the inflection block to my environments.rb file, I still get the following error on my press/new page:
“you may have ambiguous routes, or you may need to supply additional parameters for this route”
Any guesses why? Do inflectors work the same in Rails 2.0, or are they deprecated?
March 26th, 2008 at 8:55 am
I found the answer to my question, and yes, in Rails 2.x inflections have been moved from the environment.rb file to config/initializers/inflections.rb. Brad should probably make note of that in his post to keep it current.
April 9th, 2008 at 12:55 pm
Thanks for the post! I was stuck with product_infos till I found this!
May 13th, 2008 at 2:52 pm
Thanks for this, it has bugged me for a while and I have not had a chance to look into it further.
May 13th, 2008 at 4:08 pm
I have just notices another issue. Rails still expects the table to be called ‘medias’, I can’t see an obvious solution.
May 13th, 2008 at 4:16 pm
Sorry, third post in a row, turns out that Rails acts as follows with the Inflection mods in place:
>> ‘media’.tableize
=> “media”
>> ‘document_media’.tableize
=> “document_medias”
Which means that you need to put all permutations in the inflection modification.
May 14th, 2008 at 8:14 am
After a lot of searching with a colleague (Andrew Montgomery:http://darkliquid.co.uk), we came up with the following solution which seems to work in all cases (the Rails ‘uncountable’ method seems to be very unstable):
inflect.plural(/^(.*)(media)$/i, ‘\1\2′)
inflect.singular(/^(.*)(media)$/i, ‘\1\2′)
May 19th, 2008 at 4:00 pm
Media is a plural. Singular is medium.