Starting a Rails Backend

If you’re like me, when attempting to start a new app the zeroth step is always looking through previously completed projects to remember how to set up your files. While developing a rails backend is a straightforward process, much of the initial legwork happens in the command line and is therefor not committed to a format that is easily reviewable for future use. To make matters worse rails is very particular about what is pluralized, and what is singular, and what is capitalized and what is lowercase, so missteps in the initial setup are all but guaranteed. This blog aims to walk through the steps to creating a new a rails backend from scratch.

*disclaimer: This rails backend recipe is not meant to teach you how to build a rails backend, but is instead intended to be a refresher guide for developers who have already built a rails backend.

Step 1: Generate a new rails application by typing the following into the directory which is to contain your new rails app. In this example we will call the new app “rails_trails” but you can call yours anything:

rails new rails_trails— api

Step 2: Now that you have a new rails app you can have rails generate your models. For our example we will have 3 models: “Hiker”, “Trek”, and “Park”. In our example a hiker treks many parks, and a park is trekked by many hikers. A relationship like this is known as a many-to-many relationship. In this fashion we will use the Trek model as a joiner table between Hiker and Park. Trek will need to have a column called “hiker_id”, and a column called “park_id”. I will also give columns called “name” to the Hiker model and to the Park model. These table columns described above should be created at the same time, in the same CLI command as the model generation. To create your models navigate into the directory, which was created in the step above (I recommend opening it as well), and type the following CLI commands:

rails g model Hiker namerails g model Trek hiker_id:integer park_id:integerrails g model Park name

Note in the step above we distinguish the data type for the hiker_id column and the park_id column, but not for the hiker’s name column, or the park’s name column. This is because any column which will contain data stored as strings needn’t have its data-type explicitly called out. Columns containing all other data-types besides strings must have their data-types explicitly described at generation.

Step 3: Next we need to describe the associations between the models. In the model files located in the app/models directory describe the model associations. As mentioned above for my example the Hiker and Park models have a many to many relationship joined by a Trek model.

hiker.rb:

has_many :trekshas_many :parks, through: :treks

trek.rb:

belongs_to:hikerbelongs_to:park

park.rb:

has_many :trekshas_many :hikers, through: :treks

Take special note of the pluralization and singularization of the models in the snippets above. The models are plural in the hiker and park joinee models, but singular in the trek joiner model.

Step 4: If you don’t already have data you will need to create your seeds next. This should be done in a file called seeds.rb located in the db directory. A seeds file might contain something similar to the following text. You can have as many or as few of each instance as you require.

Hiker.destroy_allTrek.destroy_allPark.destroy_all
Hiker1 = Hiker.create( name: “Sean”)
Park1 = Park.create( name: “Devil’s Backbone”)
Trek.create( hiker_id:Hiker1.id , park_id:Park1.id)

Step 5: When we created our models rails also created migration files for us. Our next step is to migrate these migration files, and seed our new tables:

rails db:migraterails db:seed

Step 6: Now that we’ve populated our tables our next step is to generate our controllers. Controllers should be generated from the lowercase pluralized models like so:

rails g controller hikersrails g controller treksrails g controller parks

Step 7: With controller files built we can establish some routes. The easiest way to build restful routes without writing custom routes is to use resources. All routes can be established in the routes.rb file located in the config directory. For my app I want to be able to index, show, and create in each of my controllers. I will therefor limit my resources to these actions:

resources :hikers, only: [:index, :show, :create]resources :parks, only: [:index, :show, :create]resources :treks, only: [:index, :show, :create]

This is as far as this blog will attempt to explain but the final required step is to define the actions established in the routes above within the designated controllers. Optional steps that follow include generation and implementation of serializers, and addition of any validations that you, the developer might, feel are required.