Using Migrate and Seed in Ruby (Not Rails) & Active Record to Create a Basic Database
Published: 2022-11-19 05:58:08

To save you the read let me start with telling you a few items that will not be focused on. Do you have a basic knowledge of SQL and Ruby?  Do you also have some experience with object orientation and table associations?  Great!  I will not really be getting into those details but focusing more on how you can conceptually setup the database with Ruby and ActiveRecord.  To keep things more concise I will also skip the ActiveRecord setup details and assume you will be able to track those down easily enough.

In Ruby / Active Record the processes I will be going over are called migrate and seed.  The intent is to give you a brief overview of how to utilize these processes to streamline your setup.

Before I do that, let me distract you one more time in an attempt to give you my basic conceptual understanding of how a server application can be arranged to utilize a M V C architecture. First here is what M V C stands for:

- M = Model: this is where the the methods (aka Ruby functions) live along with the associations.  
- V = Viewer: this is the frontend most likely, it is where the user will be able to see and interact with the data as we would intend them to through some type of user interface.
- C = Controller: this is where incoming fetch requests can be handled how we want them to be.

So a little more on the model as that is the majority of the file types we will be focusing on.  Before defining any type of methods it is vital to get the basic structure of the database established.  Here is what a simple setup could be structured like:


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


You must know what data you want to work with and what type of data each will be.  Some basic associations will need to be  established so that the data can be connected between the tables.  If the associations are not setup properly your requests for data will not include all the info you want it to.


A good starting place to learn this is here: https://guides.rubyonrails.org/association_basics.html. 
The most common associations are belongs_to, has_many, and has_many through.  Although there are a few others these will likely be the most used.

Another tool to utilize are association diagrams.  They can be very helpful to visualize your flow of data and they usually look something like this: 


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


Once this has been sorted out you will then define these associations in the models.  Each model will look something like this:


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

In addition to the associations you can see that the top line is calling on ActiveRecord: :Base.  This is one of a handful of ActiveRecord setup steps that I will not get into.

Next up: need to create our tables by setting up the data fields and data types that were decided on in the association diagram.  We need to run 2 commands: 

create_migration
rake db:migrate 


The create_migration is essentially going to initialize a blank (but named) database table without any columns.  It will also create some datestamped migration files.  We will use these files in the next step.  Note that we have not actually done the migration yet.  We are getting things setup to migrate.  

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

Continuing with our migration preparation, we will take those data labels with associated data types and enter them into our newly created datestamped migration files.  In a nutshell, these entries will be the columns of your table (think like a table header).


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

Now we have our migration files setup.  Time to migrate!  Run the command rake db:migrate and your columns and column headers will now get populated.  Wohoo!


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

Ruby will expect the entries into each column to be of the datatype you specified.  On to seeding!  Creating a seed file will help you to generate some data into the rows of your columns.  You can think of it as a batch data entry of some preliminary data that you can use to test out your database in Ruby. 


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

Aside from seeding being a batch entry, you can also use it to reset your database to its original setup while testing.  For example, if you are testing the backend persistence of a delete feature you may want to repopulate the data you just deleted. Or if you are like me you may find yourself entering asdf lkjh 123 456 in your inputs for backend persistence you may end up with a bunch of garbage entries.  Reseeding will allow you roll it back to that original starting point.

The last step you should need to take is rake db:seed.  The data should then get populated and you’d be ready to start testing.  If you have a database viewer you should be able to see something like this now.


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

All done!  Hopefully this conceptual overview will be give a better understanding of the purpose of using migrate and seed.