Generated model not always autoloadable

28 views
Skip to first unread message

Peter Inglesby

unread,
Nov 11, 2012, 7:05:28 PM11/11/12
to rubyonra...@googlegroups.com
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?

Cheers,

Peter.

Godfrey Chan

unread,
Nov 11, 2012, 9:07:45 PM11/11/12
to rubyonra...@googlegroups.com
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). 

Godfrey

Sent from my phone
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Reply all
Reply to author
Forward
0 new messages