Open In App

How to create model in Ruby on Rails?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Ruby on Rails, models are like the organizers of your data. They handle interactions with your database. Think of them as the brains of your application, responsible for managing and manipulating data. They represent the structure and behavior of the information your app works with. So, whenever you need to save, retrieve, update, or delete data, models are there to help you do it smoothly and efficiently. They make it easier to work with your data in your Rails application.

For example, you might have a ‘Book’ model that keeps track of details like the book’s title, author, and publication year. This model would have methods to retrieve a list of all books, find a specific book by its title, or update information about a book.

Before creating a model, let’s create a demo project in Ruby on Rails.

Create a Demo project in Ruby on Rails

Step 1: Open the rails terminal, if you’re using Windows the terminal name will be ‘Ubuntu(WSL)’. Now run the following command. It will create a default project named myappin the current directory.

rails new myapp

Screenshot-(48)-modified

rails new myapp

Step 2 : Use this command to get into your project directory.

cd myapp

Screenshot-(49)-modified

cd myapp

Step 3 : Execute the below command in the terminal. It will create a controller file (home_controller.rb) in ‘app/controllers’ and corresponding view file (index.html.erb) which is our webpage in ‘app/views/home’. It will also create a route in ‘app/config/routes.rb’ which is needed for every webpage.

rails generate controller home index

Screenshot-(50)-modified

rails generate controller home index

Step 4 : Initially, when running the server, it gives the default welcome page as output. To display our home page, make changes in the ‘app/config/routes.rb’ file. Adding the following line in the file will set our home page as root.

root 'home#index'  

Screenshot-(51)-modified

set the home as root

Step 5 : Run the server using the command below and view the output at ‘http://localhost:3000/’. You can also view the default web page if you don’t make any change in ‘routes.rb’.

rails server

Screenshot-(52)-modified

rails server

Output

Open the browser and paste the link ‘http://localhost:3000/’

Untitled-design-(4)-modified

index.html.erb output

Create model in Ruby on Rails

Now let’s create a model named ‘book’ which will have some related attributes.

Step 1 : Execute the following command in terminal. This command creates a new model file named ‘book.rb’ in the ‘app/models’ directory and a migration file in the ‘db/migrate’ directory. It also sets up attributes for the Book model: title, author, and publication_year.

rails generate model Book title:string author:string publication_year:integer

Screenshot-(54)-modified

Step 2 : Now, you need to run the migration to create the corresponding table in your database. Run the following command in terminal. This will execute the migration file and create a books table in your database with columns for title, author, and publication_year.

rails db:migrate

Screenshot-(55)-modified

migration

Step 3 : After creating the model and migrating the database, you can start using the Book model in your application. For example, you can create a new entry, for that open the Rails console by running the command below.

rails console

Screenshot-(56)-modified

rails console

Step 4 : Now you can create rows in your table, Run the below command in rails console to create a new record in the table.

Book.create(title: "Data Structures and Algorithms Made Easy", author: "Narasimha Karumanchi", publication_year: 2016)

Screenshot-(58)-modified-(1)

Create a record

Step 5 : Now, you can interact with the existing data to use in your rails application. For example, you can query the database to retrieve records, create new records, update existing ones, or delete records. The commands should be executed in rails console. The command given below retrieves all records in Book.

Book.all

Screenshot-(59)-modified

Retrieve all records

Similarly, you can use following commands.

  1. Find books by certain conditions: Book.where(author: “Narasimha Karumanchi”)
  2. Update a record: Book.update(title: “New Title”, publication_year: 2022)
  3. Delete record that meet a condition: Book.where(author: “Narasimha Karumanchi”).destroy_all

Conclusions:

In conclusion, models in Ruby on Rails serve as the backbone of your application, managing interactions with the database and representing data structures. They simplify data handling tasks and keep your code organized, making it easier to build robust and efficient web applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads