Naming Convention in Rails

Naming Convention is always a good practice to use suggestive variable names, Class and Module name, Database table, Module and Controllers name that will assist in the documentation of a Ruby on Rails program. A good convention for variable names, Class and Module name, Database table, Module and Controllers name can often assist someone in better understanding the operation of a program module or script. So it is important to understand the naming convention for Ruby on Rails as in other programming language.

In this post I tried to explain how Variables, Class and Module, Database Table, Model etc. are defined in Ruby on Rails.

  • Variables: Variables are named where all letters are lowercase and words are separated by underscores. E.g: total, order_amount.

  • Class and Module: Classes and modules uses MixedCase and have no underscores, each word starts with a uppercase letter. Eg: InvoiceItem

  • Database Table: Table name have all lowercase letters and underscores between words, also all table names to be plural. Eg: invoice_items, orders etc

  • Model: The model is named using the class naming convention of unbroken MixedCase and always the singular of the table name. For eg: table name is might be orders, the model name would be Order. Rails will then look for the class definition in a file called order.rb in /app/model directory. If the model class name has multiple capitalized words, the table name is assumed to have underscores between these words.

  • Controller: controller class names are pluralized, such that OrdersController would be the controller class for the orders table. Rails will then look for the class definition in a file called orders_controlles.rb in the /app/controller directory.

Summary

  1. Model Naming Convention

    Table: orders

    Class: Order

    File: /app/models/order.rb

    Foreign key: customer_id

    Link Table: items_orders

  1. Controller Naming Convention

    Class: OrdersController

    File: /app/controllers/orders_controller.rb

    Layout: /app/Layouts/orders.html.erb

  1. View Naming Convention

    Helper: /app/helpers/orders.helper.rb

    Helper Module: OrdersHelper

    Views: /app/view/orders/…(list.html.erb for example)

  1. Test Naming Convention

    Unit: /test/unit/order_test.rb

    Functional: /test/functional/orders_controller_test.rb

    Fixtures: /test/fixtures/orders.yml

2 thoughts on “Naming Convention in Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s