Websites have come a long way in the last 30 years and the MVC framework has aided greatly in that. Remember older static websites with very limited, if any user interaction? I may be showing my age here, but this specific website comes to mind that CNN posted on their main page from the 1996 trial for the Orenthal James Simpson, also known as OJ Simpson:
As you can see, it is VERY simple, and not attractive at all. This came from CNN. This just goes to show how far we have come in the world of computer science. Websites have been able to shift from using simple HTML and CSS to many other frameworks that help us to create more interactive and useful websites for our users.
What is the MVC?
The Model-View-Controller is a software design pattern that software developers use to keep their code organized into three main logical components. Each component has their own role to play, which makes sense in the real world also. In a restaurant, the chef cooks the food, the server serves it, and the bartender tends bar. With everyone having their own distinct roles, things can run more smoothly and organized.
The Model:
Being in charge of all data-related logic, we can compare our Model to a Chef in a restaurant. The Chef receives the food order from the Controller (our waiter in this analogy), which our waiter received from the View (our food menu). Therefore our Chef (or Model) now has the knowledge to execute our meal. So when you think of Model, think of data-related logic.
The View:
Alright lets talk about our food menu, the View. This component of our architectural software pattern is what serves the User information they need to order a delicious meal. Being in charge of the layout and display, the view can be thought of as the frontend, or graphical user interface.
The Controller:
Our Waiter or Waitress is our Controller. This is how our Customer or User can interact with our Chef, or Model. Our middleman per se. Our Controller will collect the data and deliver it to our Model, so our Model can begin preparing our meal.
Okay, now that we know the basics of the MVC architecture, let's talk about how Active Record comes into the equation.
Rails Active Record
Rails Active Record provides an interface and binds tables in relational databases. For instance, when we think of classes with Active Record, we think of tables. Just as rows map to objects and columns map to object attributes. Every Active Record object has CRUD capabilities and methods for database access. This is known as Object Relational Mapping, which is a technique that connects the objects of an application to tables in a relational database system.
There are many benefits of using Active Record, it gives us the ability to add validations before our data is persisted to the database. It also allows us to represent associations between models and to develop inheritance hierarchies through related models.
Active Record vs SQL
When working with databases, we often have to make queries. Active Record makes this process simple for programmers. Let's compare some queries in SQL vs Active Record.
Finding Objects in SQL:
SELECT * FROM customers WHERE (customers.first_name = 'Andy') LIMIT 1
Finding Objects in Active Record:
Customer.find_or_create_by(first_name: 'Andy')
As you can see, it takes less code using Active Record and is easier for the human mind to interpret.
Say we want to find the first existing record:
SQL:
SELECT * FROM customers ORDER BY customers.id ASC LIMIT 1
Active Record:
customer = Customer.first
And the last,
SQL:
SELECT * FROM customers ORDER BY customers.id DESC LIMIT 1
Active Record:
customer = Customer.last
Active record also has a where method that allows us to make more specific queries:
Customer.where(first_name: 'Lifo')
vs SQL:
SELECT * FROM customers WHERE (customers.first_name = 'Lifo') LIMIT 1
As you can see, Active Record insulates developers from having the need to use SQL in most cases and gives us the syntactical sugar that all programmers dream of, giving us cleaner, and more readable code.
Let's look at how we can order our Objects in both Active Record and SQL:
Active Record:
Book.order(:title, created_at: :desc)
SQL:
Book.order("title ASC").order("created_at DESC")
We can also join associations with Active Record queries:
Active Record:
Book.joins(:reviews)
SQL:
SELECT books.* FROM books
INNER JOIN reviews ON reviews.book_id = books.id
Now that is a significantly less code using Active Record. Now you can see why Rails developers love Active Record so much, it saves us time, has less keystrokes and helps to enhance security in our applications.
Top comments (0)