Ruby on Rails


Naming Conventions All Versions

1.0
1.1
1.2
2.0
2.3
3.0
3.1
3.2
4.0
4.1
4.2
5.0

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 10

    Controller class names are pluralized. The reason is the controller controls multiple instances of object instance.

    For Example: OrdersController would be the controller for an orders table. Rails will then look for the class definition in a file called orders_controller.rb in the /app/controllers directory.

    For Example: PostsController would be the controller for a posts table.

    If the controller class name has multiple capitalized words, the table name is assumed to have underscores between these words.

    For Example: If a controller is named PendingOrdersController then assumed file name for this controller will be pending_orders_controller.rb.

  • 6

    The model is named using the class naming convention of unbroken MixedCase and is always the singular of the table name.

    For Example: If a table was named orders, the associated model would be named Order

    For Example: If a table was named posts, the associated model would be named Post

    Rails will then look for the class definition in a file called order.rb in the /app/models directory.

    If the model class name has multiple capitalized words, the table name is assumed to have underscores between these words.

    For Example: If a model is named BlogPost then assumed table name will be blog_posts.

  • 4

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

    app/controllers/application_controller.rb
    

    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.

    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.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Naming Conventions? Ask Question

Topic Outline