[ BNCC x GOJEK ACADEMY ] Architecture of Rails Application

Wenn
3 min readOct 8, 2018

--

Hi Guys! Here’s my second article about the lesson we learned in BNCC x GOJEK Academy. In this article, I will discuss the architecture of Rails web application.

In the previous article, I have mentioned about MVC architecture is implemented in Rails application. First coined in 1979 by Trygve Reenskaug, an MVC architecture is a design of interactive applications in which every component of the application is broken into three types: models, views, and controllers. Here’s some explanation about components in MVC architecture:

Model

Models are responsible for maintaining the state of the application. A state is referred to information provided to the application. Some of this state is transient, lasting for just a couple of interactions with the user. Sometimes the state is permanent and stored outside of the application, often in a database.

In Rails, Models are Ruby classes. Models talk to the database which means it connects the database (information) to controller (processing) . The main function of models itself is to perform business logic (query out the information needed for the business) , validate (validate the information stored in database in a standard and well-defined format), and later store data to the database (for permanent and reusable information).

Views

Views are responsible for generating a user interface (view in the browser), normally based on data in the model. Views format the data processed in the controller from model to be presented to the end user.

Rails support multiple formats of Views. You can choose to output your data in the form of HTML (complete with the CSS and Javascript), XML, JSON, or even some downloadable formats (like XLS, CSV, or PDF).

Controller

Controllers manage the interaction between Views and Models. Controllers receive events from the user (from model), interact with the model (in controller), and then call the appropriate view to display the result to the user.

Controllers in Rails are also responsible for parsing requests and handling data submissions, cookies, calling API services and sessions.

Next, we are going to talk about the database in Rails application. By default, Rails application uses relational database. A relational database is a set of formally described tables from which data can be accessed or reassembled in many different ways without having to reorganize the database tables. The standard user and application programming interface (API) of a relational database is the Structured Query Language (SQL).

In Rails application itself, it provides us with Object-Relational Model (ORM) which is a library that map database tables to Ruby classes. For example, if we have a table called “users”, our Rails application will automatically generate a class named ‘User’ that correlated to the table ‘users’. In Rails itself, we called Object-Relational Model as ActiveRecord. ActiveRecord helps us to manipulate data from the database without using SQL Query. For Example, as below

Here’s the documentation written from Rails for you to know more about ActiveRecord https://guides.rubyonrails.org/active_record_basics.html

That’s It. Thank you for reading :D

--

--

Wenn
Wenn

Written by Wenn

Google Certified Android Developer. Learning about Android, Backend technology and Algorithms.

No responses yet