Ruby on Rails: Naming Convention Cheatsheet

Carly L
2 min readJun 29, 2020

--

Database
table name => plural_of_model_name
column names => singular_snake_case
foreign_key => singularized_table_name_id
Routes
resources :controller_name_plural
resource :controller_name_singular
Model
name => CamelCase, singular (database table name is plural of model name)
file name => lowercase_singular_model_name.rb
Model Relationships
has_one/belongs_to :singular_model_name
has_many :plural_model_name
Controller
name => CamelCase, plural, append "Controller" at the end
file name => lowercase_plural_controller_name.rb
View
folder name => name of associated controller, plural
file name => action_name.html.erb
*CamelCase - first letter of every word capitalized, no spaces
*snake_case - words separated with underscore _

When you’re first starting to work with Rails, I bet you’ve wondered how is all of this working together? How does routes.rb connect to controller files where you can name actions that connect to view files? Then what you have in your view files is magically rendered onto your browser when you connect to the server. Well, you can actually find your answer somewhere hardcoded in the Rails code, but in short, developers mapped out these pathways for you and they can be easily utilized as long as you use their naming conventions.

Rails follows the principle of “convention over configuration”. Rather than creating your own rules each time, if you follow default convention, then it takes away a lot of the guess work. For example, when naming a foreign key for a User table, you could name it whatever you like e.g. UserID, UserId, dog, cat, i_like_pineapples_id, etc. But is this something worth dwelling on? Probably not. And that’s why by convention, the foreign key for a User class will be user_id by appending an _id to the name to make it simple, logical, and less complex. These type of decisions have been made for you and how folders and files associate with each other.

Model (inherits from Active Record)

name => CamelCase, singular (database table name is plural of model name)
file name => lowercase_singular_model_name.rb
has_one/belongs_to :singular_model_name
has_many :plural_model_name
-------------------------------------------------------------------
Ex) #app/models/student.rbclass Student < ActiveRecord::Base
belongs_to :teacher
end
Ex) #app/models/teacher.rbclass Teacher < ActiveRecord::Base
has_many :students
end

Routes

resources :controller_name_plural
resource :controller_name_singular
-------------------------------------------------------------------
Ex) resources :students
resource :teacher

Controller (inherits from Application Controller)

name => CamelCase, plural, append "Controller" at the end
file name => lowercase_plural_controller_name.rb
--------------------------------------------------------------------
Ex) #app/controllers/students_controller.rbclass StudentsController < ApplicationController
def index
end
end
Ex) #app/controllers/teachers_controller.rbclass TeachersController < ApplicationController
def show
end
end

View (inherits from Action View)

View 
folder name => name of associated controller, plural
file name => action_name.html.erb
-------------------------------------------------------------------
Ex) #app/views/student/index.html.erb
#app/views/teacher/show.html.erb

Since the English language can be complicated, pluralizing words is not always clean cut. Rails knows that “octopus”.pluralize => “octopi” and the reverse, “octopi”.singularize => “octopus”. However, if you wanted to create a Job class that has_many :bonuses , Rails may not associate the Job class with a Bonus class that easily “bonuses”.singularize => “bonuse” . Incorrect inflections can be fixed via config/initializers/inflections.rb and adding the following:

ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'bonus', 'bonuses'
end

Check out the documentation on inflections for more info.

Resources

--

--