what i learned last week: how to build an api utilizing ruby on rails and connecting it to a javascript frontend pt 1 
Tags: rails
Published: 2021-08-30 16:38:17

**INTRO**

Hi! In this series I'm going to go over how to build an API utilizing Ruby on Rails and connecting it to a JavaScript frontend. Remember, an [application programming interface](https://en.wikipedia.org/wiki/API) (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. 

Note, I'm learning that I code most efficiently when I work on features vertically and not horizontally. What do I mean by that? It means for the purpose of this series I'm going to through the process of creating a model, controller and view for a Player. Hmm...sound familiar? 

![MVC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lv9p71rs0u1slddiitp.png)

**API**

First, to build out and specify properties of the API head over to the terminal. As you can see I'm adding commands that tell the system that the new project being created is an API and for this example has a postgresql database. 

`rails new example_api --api --database=postgresql`

![terminal command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5q9isoa130493jkk62eo.png)

Success! Once the command has been completed you should see something like the above image in your terminal.

Next, to work with the newly created API head over to your preferred text editor and open up the newly created directory. 

![files in text editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7d9ioy6mkbg8mkokdkc.png)

Easy, right? Now that our API has been created we can start coding! As previously mentioned, I'll be using the MVC [architecture](https://www.tomdalling.com/blog/software-design/model-view-controller-explained/) to build out my program. 

**MODEL**

*The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.*

Lets get to it and head over to the terminal to build out our `Player` model and run the following command: 

`rails generate model Player username:string email:string`
![player model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95ez8ygme9t8mnq0psyw.png)
As you can see above, multiple things occurred when the above command was ran. Most importantly, a `Player` class and migration file were both simultaneously created. 

Speaking of migration file...

![migration file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu75virwtddljhw2r2ag.png)
Do you notice anything about the newly created migration file? Our Player already has a username and email attribute. 
<img width="100%" style="width:100%" src="https://media.giphy.com/media/Ri7d8I18cto2jufOKc/giphy.gif?cid=ecf05e47zjnxrq0d3hj9bsaoobo0x0vhm8hs7bnierawz4zl&rid=giphy.gif&ct=g">
Sweet, I have my player model set up let me go create an instance of it! 

![new player error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s08a1agyvrjk2yva5pw9.png)
WELP. An error?
<img width="100%" style="width:100%" src="https://media.giphy.com/media/3FbGfzApiOQC7jQeyb/giphy.gif?cid=ecf05e47fvjx84ukhhwrrze385dpq3wvcfq3e6c8b67is097&rid=giphy.gif&ct=g">
I forgot to actually create the database! In the terminal run the following command:
`rails db:create`
![database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmvrygt596yz771fil08.png)
Ok, now if I head over to the console lets see if I can create an instance of a `Player`.
![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh9m2nfig45ibegt8fn1.png)
<img width="100%" style="width:100%" src="https://media.giphy.com/media/l3q2K5jinAlChoCLS/giphy.gif?cid=ecf05e473xt7tmvogapjsld6soq3b44kplgkabm9t5wpskk2&rid=giphy.gif&ct=g">
Do you know what step I'm missing? 
![migration completed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvus9foe4p3w4f1xvpny.png)
[If](https://guides.rubyonrails.org/v3.2/migrations.html) you guessed run `rails db:migrate` in the terminal you are correct! In its most basic form it just runs the up or change method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based on the date of the migration. Note that running the `db:migrate` also invokes the `db:schema:dump` task, which will update your `db/schema.rb` file to match the structure of your database.

lets try creating a `Player` instance.
![Success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtobz5nozyasefw3fxr0.png)
<img width="100%" style="width:100%" src="https://media.giphy.com/media/3oKGzgXIbxo5KoSty0/giphy.gif?cid=ecf05e47tjubu2azfy0amzuwiovcb0sw5ip0cjn1wtl9f6uz&rid=giphy.gif&ct=g">
Success!

To review, in the first part of this series I've built out an API, created a `Player` model, set up a database and finally confirmed that the created database is up and running! In the next entry I'll be going over the controller of my `Player` model. Thank you for reading!