Intro :-ORM is used for models as well as associations between these models and data.ORM validate models before persisted to database and perform database operation via oops language. Active Record is ORM of Rails.It is M of MVC design pattern. It represent entities and data. It is used to create business object, where data is stored in persistent database. Active Record is abstraction layer which is used to access sql database via object oriented language rather than plain sql.
software requirments :-Jruby version: jruby 9.2.9.0 (compatible to ruby->2.5.7) OpenJDK 64-Bit Server VM 11.0.11+9-Ubuntu-0ubuntu2.20.04 on 11.0.11+9-Ubuntu-0ubuntu2.20.04 +jit [linux-x86_64] SQLite version 3.31.1 nodejs v10.19.0
Task :- You will create model, add records and retrieve records, which stored in sqlite db.
Step 1 -Model part
create a new Rails project.
$ rails new ti
$ cd ti
Create the model called Tiobe
$ rails generate model Tiobe language rank:integer
The generator has created a database migration file
$ rails db:migrate
A migration contains database changes. In this migration, a class called CreateTiobes is defined as a child of ActiveRecord::Migration. The method change is used to define a migration and the associated rollback. With the command rails db:migrate, you can apply the migrations,and create the corresponding database table. goto rail console $ 'rails console'
$ Tiobe.column_names
=> ["id", "language", "rank", "created_at", "updated_at"] you also get the attributes id, created_at, and updated_at by default for each ActiveRecord model. In the Rails console, you can output the attributes of the class Country by using the class method column_names. The attribute created_at stores the time when the record was initially created. updated_at stores the time of the last update for this record. id is used as a central identification of the record (primary key). The id value is automatically incremented by 1 for each new record.
Step2 -Adding Records
The method create can handle a number of different syntax constructs. If you want to create a single record, you can do this with or without brackets ({}) within the parentheses,
$ Tiobe.create(language: 'python', rank:1)
$ Tiobe.create({language: 'typescript', rank:2})
$ Tiobe.create(:language=> 'java', :rank=>3)
$ Tiobe.create('language'=> 'c', 'rank'=> 4)
In addition to create, there is also new. But you have to use the save method to save an object created with new
$ php = Tiobe.new
$ php.language = 'PHP'
$ php.rank = 5
$ php.save
You can also pass parameters for the new record directly to the method new, just as
with create.
$ Tiobe.new(language: 'Ruby', rank:6)
$ ruby.save
Step3 -Retrieve Records
In certain cases, you may need the first record or the last one or perhaps even all records. Conveniently, there is a ready-made method for each case.
$ Tiobe.first
display first record
$ Tiobe.last
display last record
Country.all is actually an Array of Country. If Country.all returns an array, then you should also be able to use iterators
$ Tiobe.all
$ Tiobe.all.each do |tiobe|
$ puts tiobe.language
$ end
Conclusion :-After performing above steps you made nano app, which create model, add records and retrieve records. By making these nano app, you also know the basics of Active Records, basic commands and migration.