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.
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
Free Resources