Generated model not always autoloadable

If you generate a model whose name ends with an underscore and then some numbers, Rails cannot then autoload the model’s class.

As an example, if you generate a model with “$ rails g model monkey_100”, this generates a file called monkey_100.rb containing a class called Monkey100. However, when you try to access Monkey100, Rails tries to find a file called monkey100.rb, and fails.

The underlying reason for this is that String#underscore is not quite the functional inverse of String#camelize. String#camelize is used to determine the class name (see NamedBase#class_name in railties/lib/rails/generators/named_base.rb) while String#underscore is used to determine the file in which the constant is expected to be defined (see load_missing_constant in activesupport/lib/active_support/dependencies.rb).

I think that Rails should check, when generating a model (and possibly other things), that the resulting class’s name is autoloadable, and fail if not. If I were to provide a patch, is it likely to be accepted?



Hi Peter,

I did some investigation on a related issue a while ago which might be helpful to you. See GH #7132, #7134 and related (I think there might be more, but it’s almost impossible to look them up on my phone right now).

The way each of the inflector methods handle multi-word input had always been a bit adhoc and not well-defined. It’s always unclear how each of these methods is going to handle multi-word input (ie how it determines where to split the words) or if it’s supposed to handle multi-word input at all. The end result is that they don’t always work well together, and every once a while when someone is trying to fix a bug in these methods they will unintentionally change how these input are handled and causes regression (depends on how you see it I guess - you can argue it’s just undefined behavior).