Ruby on Rails Naming Conventions Filenames and autoloading


Rails files - and Ruby files in general - should be named with lower_snake_case filenames. E.g.


is the file that contains the ApplicationController class definition. Note that while PascalCase is used for class and module names, the files in which they reside should still be lower_snake_case.

Consistent naming is important since Rails makes use of auto-loading files as needed, and uses "inflection" to transform between different naming styles, such as transforming application_controller to ApplicationController and back again.

E.g. if Rails sees that the BlogPost class doesn't exist (hasn't been loaded yet), it'll look for a file named blog_post.rb and attempt to load that file.

It is therefore also important to name files for what they contain, since the autoloader expects file names to match content. If, for instance, the blog_post.rb instead contains a class named just Post, you'll see a LoadError: Expected [some path]/blog_post.rb to define BlogPost.

If you add a dir under app/something/ (e.g. /models/products/), and

  • want to namespace modules and classes inside new dir then you don't need to do anything and it'll be loaded itself. For example, in app/models/products/ you would need to wrap your class inmodule Products`.
  • don't want to namespace modules and classes inside my new dir then you have to add config.autoload_paths += %W( #{config.root}/app/models/products ) to your application.rb to autoload.

One more thing to pay attention to (especially if English is not your first language) is the fact that Rails accounts for irregular plural nouns in English. So if you have model named "Foot" the corresponding controller needs to be called "FeetController" rather than "FootsController" if you want rails "magic" routing (and many more such features) to work.