In the next few articles, we’re going to share some snippets of questions and answers that we’ve been dealing with during our 12 week course office hours.
We’re putting these in blog post format so that we can help guide your research on how to become a web developer and also provide a public place where we can help you with any questions. If you have any questions, please put them in the comment section.
We’re sharing our office hour questions for a few reasons.
- The content is useful for your research in learning about web development, even if you never take a course with us.
- You will get to know Ken and Marco, your potential code mentors.
- You get an inside view of theFirehoseProject and see how our students learn and how we support them.
In one of our last office hours, a student asked a great question about the difference between a model and a controller and how to know if something should be coded within the model or controller.
Before showing you the video and answering this question, let’s go over some background in case you have no idea what a model and controller even is. Here’s what I’ll cover really quickly.
- Model, view, controller framework 101
- What is the model?
- What is the controller?
- The video from the model and advice on how to think about the difference between the model and controller
- An example of how to logically separate the difference between the model and controller
MVC Framework 101
If you’re interested in learning how to use ruby on rails (RoR) for web development, you’re likely familiar with the model, view, controller (MVC) framework that Rails and other modern web development languages uses.
If you’re not familiar with the MVC framework, here are a few resources that I’ve found helpful in explaining it in the most useful way for beginners. There has been so much written on the MVC framework that it doesn’t make sense for me to re-create anything. However, I can show you best material to read. Here are some of the best materials I’ve found on the subject . Some of the examples might not make sense until you start coding. If you want to start coding, check out this free coding pre-work.
- Railstutorial book Section 1.3.3
- Intermediate Rails: Understanding Models, Views, and Controllers. The beginning of this article is amazing but it does get a little advanced quickly. If you’re serious about web development read the entire piece, even if you don’t know exactly what they’re saying on each line.
- Stackoverflow. What is MVC in Ruby on Rails?
- Marco explains the MVC architecture in a 13 minute video here.
- Harvard’s CS50 class has a great lecture on Ruby on Rails. There’s an explanation of models, views, and controllers here from minute 14:00 to 32:20. The lecturer gets into some examples, which really help.
What Does the Model Do in Simple, Non-Coding Language?
The model is responsible for interacting with the database. When developing applications with rails, it’s often possible to to develop web applications without ever having to learn MySQL or deal with any databases directly. This is driven by the power of the model and some existing magic within the rails framework.
What Does the Controller Do in Simple Non-Coding Language?
The controller is responsible for interacting with the outside world, the model and the view to get the user all of the information that both, the model and the view, require.
In most cases, both the model and controller are used to complete related tasks. The controller is the central point of the web application and organizes everything behind the scenes before the users see it. The controller tells the model what it needs from the database and also tells the view how the whole page should look.
Watch the Full Office Hours Video
Advice From the Video
When you watch the video, you’ll see some of the best answers for the difference between the model and the controller. Fundamentally, the model “knows stuff”. It holds, the business logic of a piece of data. On the other hand, the controller, “does stuff”. It’s not responsible for understand the logic of how something is supposed to work, it just moves it around.
The main advice is simple. If the code is “doing something”, put it in the controller. If the code is “knowing something”, put it in the model.
Chess Application Example Model vs Controller
Another example that can be used to understand the difference between the model and controller is thinking about how to code a chess application. If you were building a chess application, how would you use the controller or model to code the logic of each individual piece? Would you use the controller or model to tell the application the logic of each piece?
In the best case scenario, all of the code for the logic of each piece would be in a separate model for each piece. The king, queen and pawns would all have their own model. The models would explain how the pieces move, attack and what they are worth.
The controller doesn’t know the rules for how each piece in the chess game is moved, but it’s responsible for communicating between the actual chess player and each chess piece when the actual player wants to move the pieces.
As your application gets more and more complex, you want your controller to do less and less and your model to do more and more.
Do you have any questions about this? If yes, leave them in the comment section and Marco will be able to help you out.
The person who asks the best question on this topic will get a special, and secret, prize.