Skip to content

Instantly share code, notes, and snippets.

@iangreenleaf
Last active December 20, 2024 19:50
Show Gist options
  • Save iangreenleaf/b206d09c587e8fc6399e to your computer and use it in GitHub Desktop.
Save iangreenleaf/b206d09c587e8fc6399e to your computer and use it in GitHub Desktop.
Rails naming conventions

Rails naming conventions

General Ruby conventions

Class names are CamelCase.

Methods and variables are snake_case.

Methods with a ? suffix will return a boolean.

Methods with a ! suffix mean one of two things: either the method operates destructively in some fashion, or it will raise and exception instead of failing (such as Rails models' #save! vs. #save).

In documentation, ::method_name denotes a class method, while #method_name denotes a instance method.

Database

Database tables use snake_case. Table names are plural.

Column names in the database use snake_case, but are generally singular.

Example:

+--------------------------+
| bigfoot_sightings        |
+------------+-------------+
| id         | ID          |
| sighted_at | DATETIME    |
| location   | STRING      |
| profile_id | FOREIGN KEY |
+------------+-------------+

+------------------------------+
| profiles                     |
+---------------------+--------+
| id                  | ID     |
| name                | STRING |
| years_of_experience | INT    |
+---------------------+--------+

Model

Model class names use CamelCase. These are singular, and will map automatically to the plural database table name.

Model attributes and methods use snake_case and match the column names in the database.

Model files go in app/models/#{singular_model_name}.rb.

Example:

# app/models/bigfoot_sighting.rb
class BigfootSighting < ActiveRecord::Base
  # This class will have these attributes: id, sighted_at, location
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
  # Methods follow the same conventions as attributes
  def veteran_hunter?
    years_of_experience > 2
  end
end

Relations in models

Relations use snake_case and follow the type of relation, so has_one and belongs_to are singular while has_many is plural.

Rails expects foreign keys in the database to have an _id suffix, and will map relations to those keys automatically if the names line up.

Example:

# app/models/bigfoot_sighting.rb
class BigfootSighting < ActiveRecord::Base
  # This knows to use the profile_id field in the database
  belongs_to :profile
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
  # This knows to look at the BigfootSighting class and find the foreign key in that table
  has_many :bigfoot_sightings
end

Controllers

Controller class names use CamelCase and have Controller as a suffix. The Controller suffix is always singular. The name of the resource is usually plural.

Controller actions use snake_case and usually match the standard route names Rails defines (index, show, new, create, edit, update, delete).

Controller files go in app/controllers/#{resource_name}_controller.rb.

Example:

# app/controllers/bigfoot_sightings_controller.rb
BigfootSightingsController < ApplicationController
  def index
    # ...
  end
  def show
    # ...
  end
  # etc
end
# app/controllers/profiles_controller.rb
ProfilesController < ApplicationController
  def show
    # ...
  end
  # etc
end

Routes

Route names are snake_case, and usually match the controller. Most of the time routes are plural and use the plural resources.

Singular routes are a special case. These use the singular resource and a singular resource name. However, they still map to a plural controller by default!

Example:

resources :bigfoot_sightings
# Users can only see their own profiles, so we'll use `/profile` instead
# of putting an id in the URL.
resource :profile

Views

View file names, by default, match the controller and action that they are tied to.

Views go in app/views/#{resource_name}/#{action_name}.html.erb.

Examples:

  • app/views/bigfoot_sightings/index.html.erb
  • app/views/bigfoot_sightings/show.html.erb
  • app/views/profile/show.html.erb

More resources

@EdwinRozario
Copy link

EdwinRozario commented Jun 29, 2020

This is an odd one. How about routes. I saw this in a routing file, controller action named with ?. Couldn't say if thats the right way to go or not.

post '/sign_up/valid_provider', to: 'sign_up#valid_provider?'

@aesyondu
Copy link

aesyondu commented Jul 3, 2020

Is there a naming convention for ActiveRecord Callbacks? Is it just verb_noun?

https://guides.rubyonrails.org/active_record_callbacks.html

@jeaninerenee
Copy link

Love. This is very helpful for me as a newbie to Ruby and Rails. Thank you!

@gonzabt3
Copy link

if you have a route with long name.
example: https://localhost/long_name_route/1
Is this route path with underscore correct?

@jacinyan
Copy link

This is absolutely amazing!!

@manuel1280
Copy link

manuel1280 commented Dec 16, 2020

There is some validation about reserved names or something similar? for example I need to name a model "application_credential", could this break some of rails?, since rails name all with the prefix 'application_ ...'

@iangreenleaf
Copy link
Author

iangreenleaf commented Dec 16, 2020

There is some validation about reserved names or something similar? for example I need to name a model "application_credential", could this break some of rails?, since rails name all with the prefix 'application_ ...'

I would just check the Rails API docs and see if there's a class with the name you want to use. If there is, pick a different name for your class so as not to create conflicts. If you don't have control over your database table names, you can always use .table_name to override the inferred table name.

There are a few column names (class and end are the most common) that cause problems because they are reserved words. It's possible to work around these, but best to avoid naming columns that if possible.

@bashunaimiroy
Copy link

A big nota bene here is that this gist contains one inaccuracy: PascalCase is where the first letter of every word is uppercase, camelCase keeps the first letter of the first word lowercase. Further reading here. Thanks very much for writing this though.

@sabrina214
Copy link

How does rails decide how to pluralize some resource, what does it use internally for this? For eg, whether the plural form will end with 's', or get replaced by 'ies', or remains same for words whose singular and plural forms are same?

From where did rails took it's English grammar classes? 😅😅

@bashunaimiroy
Copy link

Hi @sabrina214 . You can define further rules in the Inflector. This should have some answers for you: https://medium.com/@anna7/ruby-on-rails-pluralization-b3927de2ca8e

@clint326
Copy link

I didn't see a comment, but scaffold's should be Singular (i.e., $ rails g scaffold Post title:string body:text). Maybe that could be added.

@pedromaia1218
Copy link

Very useful! Thanks

@gutoarraes
Copy link

I came looking for the scaffold referrence. Thank you @clint326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment