Ruby on Rails Explained
The Model

May 31, 2023

In the Model-View-Controller (MVC) architecture, the Model is responsible for representing the data and the business logic of an application. In Ruby on Rails, the Model is implemented using the ActiveRecord pattern, which is a powerful and easy-to-use ORM (Object-Relational Mapping) tool. In this article, we will explore the Model part of MVC in Rails and provide code examples to illustrate its usage.

To create a Model in Rails, we can use the rails generate model command. For example, let's say we want to create a User model with name and email attributes. We can execute the following command in the terminal:

rails generate model User name:string email:string

This will generate a user.rb file in the app/models directory with the following code:

class User < ApplicationRecord
end

The User class extends ApplicationRecord, which is a subclass of ActiveRecord::Base. This gives us access to many ActiveRecord methods and functionalities.

To add validations to our User model, we can use the validates method. For example, we can require the presence of the name and email attributes by adding the following code to the User class:

class User < ApplicationRecord
  validates :name, presence: true
  validates :email, presence: true
end

Now, if we try to create a User record without providing a value for either name or email, Rails will raise a Validation failed error.

To create a new User record, we can use the following code:

user = User.new(name: "John Doe", email: "john.doe@example.com")
user.save

This will create a new User record in the database with the specified values for name and email.

To retrieve data from the database, we can use various ActiveRecord querying methods. For example, to retrieve all User records, we can use the all method:

users = User.all

This will return an ActiveRecord Relation object containing all User records in the database.

We can also use conditions to filter the results. For example, to retrieve all User records with a specific name, we can use the where method:

users = User.where(name: "John Doe")

This will return an ActiveRecord Relation object containing all User records with the name "John Doe".

In addition to querying data, ActiveRecord also provides methods to update and delete records. For example, to update a User record, we can use the update method:

user = User.find(1)
user.update(name: "Jane Doe")

This will update the name attribute of the User record with an id of 1 to "Jane Doe".

To delete a User record, we can use the destroy method:

user = User.find(1)
user.destroy

This will delete the User record with an id of 1 from the database.

In conclusion, the Model part of MVC in Rails is implemented using the ActiveRecord pattern, which provides powerful ORM functionalities for representing data and business logic in an application. With ActiveRecord, we can easily create, read, update, and delete records in the database, as well as add validations and perform complex queries. By understanding how to use the Model in Rails, we can build robust and scalable applications with ease.

Code Example Output:

 # Creating a new User record
user = User.new(name: "John Doe", email: "john.doe@example.com")
user.save

 # Retrieving all User records
users = User.all
 #=> [#<User id: 1, name: "John Doe", email: "john.doe@example.com", created_at: "2023-06-01 00:00:00", updated_at: "2023-06-01 00:00:00">]

 # Retrieving User records with a specific name
users = User.where(name: "John Doe")
 # => [#<User id: 1, name: "John Doe", email: "john.doe@example.com", created_at: "2023-06-01 00:00:00", updated_at: "2023-06-01 00:00:00">]

 # Updating a User record
user = User.find(1)
user.update(name: "Jane Doe")

 # Deleting a User record
user = User.find(1)
user.destroy