Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

@lcpriest

This comment has been minimized.

Copy link

@lcpriest lcpriest commented Nov 14, 2015

delete -> destroy for routes, right?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 13, 2016

👍

@bastilimbach

This comment has been minimized.

Copy link

@bastilimbach bastilimbach commented Apr 27, 2016

Thank you. This was very helpful!

@BKSpurgeon

This comment has been minimized.

Copy link

@BKSpurgeon BKSpurgeon commented Jul 15, 2016

what if i'm naming a resource

i.e.

rails g resource job_types name

is the job_types correct or should it be singular, or should it be camel case etc?

what about attributes: should they be plural or singular?

@geein

This comment has been minimized.

Copy link

@geein geein commented Oct 15, 2016

Thank you so much!!!1

@boguz

This comment has been minimized.

Copy link

@boguz boguz commented Oct 20, 2016

nice. thanks! =)

@suthanbala

This comment has been minimized.

Copy link

@suthanbala suthanbala commented Dec 7, 2016

Thanks! There's a minor typo at the beginning on your first paragraph "or it will raise and exception instead of failing", I believe that and should be an an

@BKSpurgeon

This comment has been minimized.

Copy link

@BKSpurgeon BKSpurgeon commented Jan 13, 2017

Hi apparently can't submit a pull request on a gist.............anyways i've added a migration based convention section perhaps you may find it of benefit. here it is in case you want to add to it.

Migration Based Conventions

rails generate migration add_fieldname_to_tablename fieldname:string

or

rails generate migration AddPartNumberToProducts

  • Please remember that the table name is pluralized
  • This creates a migration based on the above settings
@brunoocasali

This comment has been minimized.

Copy link

@brunoocasali brunoocasali commented May 8, 2017

You could add a Concern part in your doc! :D

@arunkmoury

This comment has been minimized.

Copy link

@arunkmoury arunkmoury commented Nov 24, 2017

Thanks but I am confused regarding resource name. Should resource name match to any database model or table or any entity of rails project?
Or we can put any name to resource. Please explain apart from snakecase and camelcase

@pradhumnsharma

This comment has been minimized.

Copy link

@pradhumnsharma pradhumnsharma commented May 7, 2018

Really helpful, thanks for uploading this content. It helps beginners like me very much.

@say8425

This comment has been minimized.

Copy link

@say8425 say8425 commented Jun 25, 2018

It's really great post! Could I translate it to Korean?

@chevyconner

This comment has been minimized.

Copy link

@chevyconner chevyconner commented Nov 17, 2018

if i have model and controller namely address and addresses, will it work flawlessly?

@coachben

This comment has been minimized.

Copy link

@coachben coachben commented Feb 18, 2019

Quite a saver. thank you

@Levii01

This comment has been minimized.

Copy link

@Levii01 Levii01 commented Mar 13, 2019

Tanks, useful :)

@yu-coder

This comment has been minimized.

Copy link

@yu-coder yu-coder commented Jan 18, 2020

This is a nice post!

@Jegannathan

This comment has been minimized.

Copy link

@Jegannathan Jegannathan commented May 5, 2020

According to the standard, I believe avoiding an underscore in the routes is less error-prone https://restfulapi.net/resource-naming/
can you guide us on this? do you think still it needs underscores in routes?

@iangreenleaf

This comment has been minimized.

Copy link
Owner Author

@iangreenleaf iangreenleaf commented May 5, 2020

@Jegannathan good question! Rails is still using underscores by default, so my recommendation would be to prefer the Rails convention over other conventions. Unless, of course, an app already uses dashes for routing, in which case consistency probably trumps convention.

@Jegannathan

This comment has been minimized.

Copy link

@Jegannathan Jegannathan commented May 5, 2020

@iangreenleaf thanks for your response. Refered Some RFC docs come to know they prefer spinal case more than snake case. they modified in rfc3986 and they are insisting hyphen. https://tools.ietf.org/html/rfc3986#appendix-D.2. But i don't know why ruby still prefers underscore

@Fermn

This comment has been minimized.

Copy link

@Fermn Fermn commented Jun 10, 2020

when you initialize a rails application, does the app name need to be camelcase, anyone?

@iangreenleaf

This comment has been minimized.

Copy link
Owner Author

@iangreenleaf iangreenleaf commented Jun 19, 2020

@Fermn rails new takes a path for the app files and will convert that into camel case for you. Most of the rails generators are smart about converting between snake case and camel case as needed.

@EdwinRozario

This comment has been minimized.

Copy link

@EdwinRozario 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

This comment has been minimized.

Copy link

@aesyondu 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

This comment has been minimized.

Copy link

@jeaninerenee jeaninerenee commented Jul 15, 2020

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

@gonzabt3

This comment has been minimized.

Copy link

@gonzabt3 gonzabt3 commented Aug 26, 2020

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

@jacinyan

This comment has been minimized.

Copy link

@jacinyan jacinyan commented Sep 21, 2020

This is absolutely amazing!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.