Building a Rails Backend
In this blog post I am going to be demonstrating how to build a basic backend using Ruby on Rails. Rails is an easy language to learn luckily for beginners Rails does a lot of stuff for you, unlike Node rails creates your file structure and your controllers, models, and migrations with the use of a few simple commands.
To start out you’ll need to use your terminal, my terminal of choice is ITerm2. If you’d like to use this you can Install ITerm2 by going to https://www.iterm2.com/ then click “Download” at the bottom, and follow the instructions (moving it into your Applications folder when installation is completed).
You’ll also need to install Ruby once you have your local terminal setup: To install Ruby: a) First check if Ruby is installed by entering “ruby -v” in your terminal (ITerm2). If it is installed, you will see a version such as “ruby 2.6.1p33”. b) For installation instructions, if needed, see https://www.ruby-lang.org/en/documentation/installation/
Now that you have a terminal and ruby on your computer we can begin building a rails app. The first command to know is going to set you up a Rails app that comes with a git repository built into it. Type in the command:
This creates a Rails app that will be named “dog-app” make sure to include the - - api at the end. Now that you’ve created this app cd into it and open it in your text editor of choice. Mine is Visual Studio Code. Now you can begin creating your controllers, models, and migrations for your app. For the sake of this app we are going to only use dogs that are pure bred so that the relationship I am going to build makes more sense. For this app each dog will have only one breed but a breed can have many dogs. To make these files with the desired attributes of your class type in the command:
Let’s break down the picture above, rails g is what is creating the files. The g stands for generate, in rails if you’d like you can type this out as “rails generate” I choose to use “rails g” because it is faster, the “resource” is what is going to make the controller, model, and the migrations. In rails you can create the model and the controller separately with “rails g model” or “rails g controller”, once again I chose to use this because it is faster. The first word that comes after resource here is what the name of this table and class is going to be in your backend/database and the following term is an attribute of that class/column of the breed table. In rails the default data type for an attribute is a string, if I want something that’s an integer, say age for example I would type “rails g resource breed name age:integer” as you can see you need a colon and the datatype now. Now for this example I’m building a one to many. So I am going to build my next class/table:
Now with this table you’ll notice the breed:references, that’s because of how this relationship is set up. A Breed has many dogs but a dog belongs to one breed (or in this example). Since a dog belongs to a breed you give it the references. From running these commands you’ll see that quite a few files have been created:
Now if you go to the app file in your Rails apps file structure to the side you’ll find a models and controllers folder with the things you just created inside them:
Now in the models if you go to dog.rb you’ll notice that it’s a little different from breed.rb:
The belongs_to :breed in here came from our breed:references attribute that we typed in earlier. Now in the Breed model we’re going to add one easy line of code to finish building this relationship:
Now that this relationship is built you can see and verify your attributes are what you desired by checking your migration files in the db/migrate folder:
Since all of this checks out we can now make a couple seeds for the database. In the db folder you should see a seeds.rb file. In here is where we can begin creating some instances to put in the database:
As you can see here when we create an instance of a dog it has a reference to a breed. You will also see the Dog.destroy_all and Breed.destroy_all, these are here so that if you need to reseed your database the old one is destroy and all of the seeds are wiped clean so that you start with a blank sheet. Now that you have made a couple seeds you’ll need to run a few commands.
This will officially create and migrate the database tables you created earlier. and you’ll have to run this command:
This will add the seeds from your seed file into the database.
We have now successfully create a backend with some tables, some seeds, and a relationship built. The next steps would be to add your actions to each of the controllers so you can perform CRUD on these classes, and if you wish to link this to a frontend browser web app you would need to uncomment out CORS(cross origin resource sharing). In this blog post I won’t get to CRUD and CORS, but keep an eye open for future posts about it!