This project is archived and is in readonly mode.

#4704 ✓resolved
Juanma Cervera

Router in Rails 3 pluralizes collection named routes twice

Reported by Juanma Cervera | May 26th, 2010 @ 11:42 AM | in 3.0.2

I have inflections for the Spanish language.
A model named 'Cliente' pluralizes to "clientes" and this should be the name of the controller.
Everything works well with the inflections, except with the new router.

With the new router

NewApp::Application.routes.draw 
  resources :clentes
end
This fails, it generates routes for a controller named "clienteses" and that it is not correct.

And I have to use the old router, using

NewApp::Application.routes.draw do |map|
  map.resources :clentes
end

An this works, but I can use the improved features of the router.

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino May 26th, 2010 @ 05:04 PM

    • State changed from “new” to “invalid”

    You have to fix this in your application doing your own inflections. Thanks.

  • Juanma Cervera

    Juanma Cervera May 27th, 2010 @ 07:48 AM

    Hi Santiago.
    I don't know what you mean.
    I already have an initializer with the inflections for the language of my app.

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.plural( /$/, 'es')
      inflect.plural(/z$/i, 'ces')
      inflect.plural(/([aeiou])$/i, '\1s')
    
      inflect.singular(/s$/i, '')
      inflect.singular(/es$/i, '')
      inflect.singular(/ces$/i, 'z')
      inflect.singular(/([tj])es$/i, '\1e')
    
    end
    

    And it works great, for all the application, except in the routes, and only when I use the new router syntax.
    Also works well when I use the old router syntax, and this make me think of a problem.

    Juanma Cervera

  • Andrew White

    Andrew White June 7th, 2010 @ 11:42 AM

    If you compare the deprecated mapper to the new mapper then you can see there's an extra call to pluralize which is causing the problem.

    My Spanish isn't up to the task but if you adjust the first inflect.plural to not match words ending 'es' then that should fix your problem as invoking pluralize on English plural words doesn't double pluralize them.

  • gucki

    gucki June 14th, 2010 @ 12:24 PM

    I'm facing the same error, using a fresh rails project using latest beta4 and no custom inflections. Please see here:
    https://rails.lighthouseapp.com/projects/8994/tickets/4857

  • javierm

    javierm August 2nd, 2010 @ 05:48 PM

    My Spanish isn't up to the task but if you adjust the first inflect.plural to not match words ending 'es' then that should fix your problem

    Hello, Andrew.

    I'm spanish as well and am using similar inflection rules, so I've got the same problem.

    Unfortunately, your suggestion isn't free of troubles. In spanish, there are words that are singular and end in "s", like "res", whose plural is "reses" (same rule as any other word ending in a consonant).

    I know I can add irregular plurals, but then I guess that if we use them for regular plurals we miss the point of defining expressions about how to make plurals.

    This bug is mark as "invalid", but I don't think it should be. "wontfix" maybe, but Rails is supposed to have rules such as "singular form for member path and plural form for collection path", and here it is creating "singular form for member path and plural of the plural for collection path", because of the extra call to pluralize that you mention.

    Thanks!

  • javierm

    javierm August 2nd, 2010 @ 09:03 PM

    Apparently it also happens with english words, even if I don't use any inflections.

    The plural of "taxi" is "taxis", but the plural of "taxis" is "taxes". So if I generate a "taxi" scaffold, "taxis_path" is not generated, but "taxes_path" is.

    Unfortunately I haven't been able to come with a valid test case. Test cases for taxes are already there, and adding test cases for taxis ends up in failure, but I think it fails because of a conflict with the already defined routes for taxes.

    I hope you take this as "trying to help" instead of "spamming on that thing nobody cares about", cause that's my intention ;). If you don't want to fix it, hey, it's your app, you have your priorities.

  • javierm

    javierm August 4th, 2010 @ 09:00 PM

    • Title changed from “Router in Rails 3 doesn't use inflections properly” to “Router in Rails 3 pluralizes collection named routes twice”

    I think I finally figured out where to write the test I wanted. I attach a failing test, patched against v3.0.0_RC (sorry, I couldn't get master to work; bundle failed).

    The first assertion works, but the other two fail.

    Thanks.

  • Andrew White

    Andrew White August 5th, 2010 @ 07:45 AM

    • State changed from “invalid” to “open”
    • Assigned user set to “Andrew White”
    • Importance changed from “” to “Low”

    The problem with your test is that taxis (as in the minicab) doesn't have it's own rule - once you add the custom irregular inflection everything works out and it's unlikely you're going to be writing an app with deals with minicabs and surgery together. What we need is a test where two words are likely to be used together and their pluralization overlaps similar to taxi(s) and tax(is|es) - it doesn't matter if they're Spanish.

    Anyway, it's really a good idea to get your inflections setup so that they are idempotent as you never know when you'll end up pluralizing an already pluralized word in your own code.

  • javierm

    javierm August 5th, 2010 @ 09:06 PM

    Hi, Andrew.

    Didn't see your answer before, but in the meantime I've created a patch that makes the test pass and doesn't break any other tests (at least in action pack; couldn't test the whole suite):

    http://gist.github.com/510293

  • javierm

    javierm August 5th, 2010 @ 09:18 PM

    What we need is a test where two words are likely to be used together and their pluralization overlaps similar to taxi(s) and tax(is|es) - it doesn't matter if they're Spanish.

    I'm afraid I don't understand what you mean. Since I'm not sure we're talking about the same thing, I'll try to explain my problem.

    The problem happens with almost any spanish word. The plural of "cliente" is "clientes". And grammar rules says that, if it existed, the plural of "clientes" would be "clienteses" (and the plural of "clienteses" would be "clienteseses", and so on), since words ending with a consonant make (usually) the plural with "-es",

    And that's the original problem: applying spanish rules to the inflections made "clienteses_path" to be generated, instead of "clientes_path".

    Hope it helps. Tell me if I'm understanding you wrong.

    Thanks.

  • javierm
  • Andrew White

    Andrew White August 18th, 2010 @ 01:45 PM

    • Milestone cleared.
    • Tag set to rails 3.0, patch, resources, routing

    Javier, I've taken your patch and expanded it a bit - don't worry you're still marked as the author.

    Anyway the attached patch tests both the medical term and transport term and refactors the resource name methods a bit. The patch applies cleanly to master and stable.

  • javierm

    javierm August 18th, 2010 @ 04:03 PM

    Awesome! Thank you, Andrew.

    It's ok if you figure as the author anyway. I wouldn't have come up with such a complete patch, after all.

  • Repository

    Repository August 18th, 2010 @ 05:58 PM

    • State changed from “open” to “resolved”

    (from [3e871eee8048c0e11e270ae4fbcbe40226148c33]) Don't pluralize resource methods [#4704 state:resolved]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    http://github.com/rails/rails/commit/3e871eee8048c0e11e270ae4fbcbe4...

  • Repository
  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages