Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby on rails
databases

What is active record pattern in ROR?

Behzad Ahmad

The Active Record Pattern is used to access the data stored in a relational database. It allows us to create, read, update, and delete data from a database. It is particularly used for persistently stored data.

The Active Record Pattern is a part of the MVC design pattern.

Basic functionalities

  • Make a connection with the database.
  • Create tables in the database.
  • Specify the relations of tables and classes.
  • Perform CRUD operations.
  • Make object relational mapping between classes, objects, rows, tables, etc.

How to create Active Record Model

If we want to create an Active Record Model, the model must first be inherited from the ApplicationRecord class, as follows:

class Student < ApplicationRecord

end

The statement above will create a class named Student. If we want to create a table of that class in order to store some data, we can also create a table using SQL:

CREATE TABLE Student (
  varchar rollNo(11) NOT NULL auto_increment,
  name varchar(50),
  subject varchar(100),
  PRIMARY KEY  (rollNo)
);

Now, a table with 3 attributes rollNo, name, subject is formed. We are able to add data to our tables:

temp = Student.new
temp.name = "Behzad Ahmad"
puts temp.name 
temp.subject = "Software Engineering"
puts temp.subject 

RELATED TAGS

ruby on rails
databases

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring