Getting started, Sinatra Backend with Active Record(Ruby)
Published: 2022-06-02 19:16:52

I just want to give a brief overview on how one can get started building a backend that uses Sinatra with Active Record coding in Ruby. With this blog there are some assumptions that one will know the frontend CRUD to talk to the backend. Let's start with a new VS code window. 

We are going to create a bunch of folders and files. For now let's go off this screen shot. Everything on here is empty so we will go into the purposes of each and fill them out as we go.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg5utz8788nlb3uag6a3.png)
 
Let s start with our Rakefile.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xb6o6zz73wkm9egqffcx.png)
 
What is happening here is the first description will start the server for us once we enter into the console and type rake server. The next two lines are designating a port in which to run the local server on. Then the next line rerun allows auto-reloading of server when files are updated. Finally the last line allows us to call rake console in the terminal to enter into Pry.

Now our Gemfile. I'll leave in the comments as there own descriptions of each gem.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nl481i613stirsqcuxc.png)

Our config.ru will look like this. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgc8j9wxn7v3oi2gi8ci.png)
 
Rack::Cors allows for Cross-Origin Resource Sharing request. Rack:JSONBodyParser will parse the json from the request body into params hashes.
 
Next we are going to go all the way up to the config/ environment.rb. We will not use the db migrate until we start building our tables.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nn0adi4ocm7pl1iu2zl.png)
 
The first line is an environment variable that is used by some of the Rake tasks to determine if our application is running locally in development, in a test environment, or in production. The next two lines require our Gems.The last line requires all files in 'app' directory.

Finally we just need to create the inheratence relationship in our Application Controller.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pxy95aonelj3oe9uq3f.png)

Alright now lets create some tables to work with. Open your terminal and cd into Server. Run "bundle install". That will install all of our gems. Now we can create our tables. We will create a table called "humans". Type "rake db:create_migration NAME=humans". Once you hit enter you will see that the terminal will display that there is a new file in the migrate folder. You will also see it in the explorer with the timestamp that it was created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2atpz4v7ihrakzxnhn6y.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avaxdcq9fydb7ju9b7fh.png)

Remember that we need to be very specific with our naming conventions. Models and Classes are singular. Tables and schema are plural.

Let's create another table called "dogs". Then we will create a relationship between the two and build the tables. Type "rake db:create_migration NAME=dogs" into the teminal. Alright now we have two tables to work with. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/076y5cuo0ex6wv5p4tqm.png)

In our humans migrate file we will create a table using the "create_table method. It will look like the following to give us three columns. The id will be the first column and that will be auto generated. The second will be their first name and the last will be their last_name.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96pndolvo738t2jgersg.png)

Now lets work on our dogs. It will look very similar but we will add a foriegn key to establish a relationsip between a dog and their human.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqj8fkishfh4a8294mny.png)
 
Now we can run "rake db:migrate" to create our SQLite tables. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i4ulbq4tvtpr7hiy7y7.png)
 
We can now see the schema that was created by active record as well.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljqpmxsc7cylfzpo71w6.png)
 
In app/models we build the classes for dogs and humans. Remember their classes will be singular. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myf7g4n2zxoqxl43zrgu.png)
 
Our classes will look like the following inheriting from ActiveRecord::Base. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb83321njz68611cla8u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38lubgheg93hhy9dr6d0.png)

Now we have the base for our Sinatra backend. Next blog we will go over how to create instances of the classes to populate data. Also how to create a seed file to add data. Then how to use the Application Controller to allow for CRUD.