All filenames are in snake_case following the same conventions
- Model: singular (e.g.
Restaurant) - Controller: plural (e.g.
RestaurantsController) - Table in DB: plural (e.g.
restaurants) - URL's: all in plural (e.g.
/restaurants,/restaurants/:id,/restaurants/new)
- Create model: singular (because the name of the model is singular). e.g.
rails g modelRestaurantname:string rating:integer - Create migration: plural (because we use the name of the table). e.g.
rails g migration AddDescriptionToRestaurantsdescription:text - Create controller: plural e.g.
rails g controllerRestaurantsindex show
All in singular, because all ActiveRecord's methods are linked to the model.
Examples:
Restaurant.allRestaurant.create(name: "Burger King", rating: 2)Restaurant.find(params[:id])
- Singular in the
belongs_to. Because it belongs to one element. - Plural in the
has_many. Because it has many elements.
e.g.
class Restaurant < ActiveRecord::Base
has_many :reviews
end
class Review < ActiveRecord::Base
belongs_to :restaurant
endPlural when defining a route for a resource:
resources :restaurantsindex: plural (because we are showing a list of elements). e.g.restaurants_path. Can also be used used forcreate.show: singular (we are showing just one element, it needs the element inside parenthesis). e.g.restaurant_path(@restaurant). Can also be used forupdate&delete.new: singular. e.g.new_restaurant_path