The Basics: What is MVC?

Jacinta Calixte
3 min readOct 29, 2020

--

In the first part of this series we created a simple rails app. Now we will go over what MVC is. If you haven’t run into MVC prior to this blog, let be be the first to tell you what it stands for. Right before I do, let me explain another acronym that I tend to confuse with MVC, which is MVP. MVP stands for minimum viable product. The technical definition of MVP is “development technique in which a new product or website is developed with sufficient features to satisfy early adopters.” In layman's terms it's the basic functions of an application. Using Youtube as an example, the MVP would be that Youtube is app that allows the user to upload and share videos. Now that you know what MVP is, I hope you never confused the two like I did.

Let’s get into MVC! MVC stands for Model, Viewer and Controller. Each part of this structure has a very important job. The Model’s job is to maintain the relationships between the database and the objects. The Model is where the logic behind the application lives. The Viewer is the data being displayed to the user. What is displayed to the user in the Viewer has no programing logic. Lastly, there is the Controller. The Controller's job is tell the data where to go. The controller takes the data that it receives from our stick figured user shown above, brings it to the model, and then retrieves and delivers data that is then shown in the Viewer to the user. The device the user is using displays what is in the viewer and the Controller brings the data to the Viewer.

Since we are building a rails application (see The Basics: Let’s Make A Rails APP! for the first blog of the series) You're probably wondering what code goes inside each part of the MVC structure. In rails you can run rails g resource to generate the models, associations,attributes and foreign keys. If you are using a string for the value of the column name there is no need to write the datatype. This is how you would write it if you have one model.

Here is an example where I have an Events model that belongs to an artist with the attribute of name and date. In my Events model, my date is a string. I could’ve also written the last part of this code as artist:belongs_to instead of artist_id:integer. Both versions of this code work the same in which they creates the association between events and a model I had previously create which was called Artist.

If you want to break down the rails g resource generator and to it seperate each task, you can by running the command rails generate to see which options you have for generators. Below I have a example of me running that code and showing you what generators exists. I believe that resource is the best, especially if your working on a new application.

Once you have fill in the generator with your models, data types and associations, it will generate the model file, the migration file, the controller file and view files and routes. That is way better than having to manually create those files using other generators. In the next blog we will be talking about Active record: Associations in Rails. Understanding Associations will help you build out relationships which you can be used with these generators.

Happy coding!

--

--