Rails ORM-3

Rails ORM-3

DevNation's photo

Published on Jul 18, 2021

3 min read


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.


You will create model, add records via seeds.rb and run sql queries.

software requirments:-

jruby (compatible to ruby->2.5.7),
rails >6,
OpenJDK 64-Bit Server VM 11.0.11+9,
Ubuntu-20.04 +jit [linux-x86_64]
nodejs v10.19.0




Knowledge of ruby language and text editor

Step 1-

Creating Model- please refer


Populating the Database with seeds.rb

Tiobe.create(language: "python", publish_year: 1980)
Tiobe.create(language: "typescript", publish_year: 2012)
Tiobe.create(language: "java", publish_year: 1995)
Tiobe.create(language: "c", publish_year: 1975)
Tiobe.create(language: "php", publish_year: 1995)
Tiobe.create(language: "ruby", publish_year: 1995)
Tiobe.create(language: "csharp", publish_year: 2000)
Tiobe.create(language: "kotlin", publish_year: 2011)
Tiobe.create(language: "swift", publish_year: 2014)

please refer


Write sql queries

The simplest case is searching for a record via a primary key (by default, the id field in the database table). If I know the ID of an object, then I can search for the individual object or several objects at once via the ID.
$ rails console

$ Tiobe.find(2)

=> # 09:57:02.000000000 +0000", updated_at: "2021-07-18 09:57:02.000000000 +0000">

$ Tiobe.find([1,3,7])

With the method where, you can search for specific values in the database. $ Tiobe.where(publish_year: 1995)

$ Tiobe.where(publish_year: 1995).count

$ Tiobe.where(publish_year: 1995..2014).count

$ Tiobe.where(publish_year: 1995..2014, id: 1..7).count

$ Tiobe.where(publish_year: 1995..2014, id: 1..9).count

$ Tiobe.where.not(publish_year: 1980).count

$ Tiobe.where(publish_year: 1975).or(Tiobe.where(language: 'python')).count

(2.4ms) SELECT COUNT(*) FROM "tiobes" WHERE ("tiobes"."publish_year" = ? OR "tiobes"."language" >= ?) [["publish_year", 1975], ["language", "python"]] => 2

$ Tiobe.where(publish_year: 1975..1995).sum(:publish_year)

(1.2ms) SELECT SUM("tiobes"."publish_year") FROM "tiobes" WHERE "tiobes"."publish_year" >BETWEEN ? AND ? [["publish_year", 1975], ["publish_year", 1995]] => 9940

$ Tiobe.where(publish_year: 1975..1995).order(:language).sum(:publish_year)

$ Tiobe.where(publish_year: 1975..1995).order(:language)

$ Tiobe.where(publish_year: 1975..1995).order(:language).reverse_order

$ Tiobe.where(publish_year: 1980..1995).limit(3)

$ Tiobe.order(:language).limit(5)

With the method group, you can return the result of a query in grouped form.

Normally, ActiveRecord pulls all table columns from the database and leaves it up to programmers to later pick out the components they are interested in. But when you have a large amount of data, it can be useful and, above all, much quicker to define a specific database field directly for the query. You can do this via the method pluck.

$ Tiobe.where(publish_year: 1975..2014).pluck(:language)

(1.3ms) SELECT "tiobes"."language" FROM "tiobes" WHERE "tiobes"."publish_year" BETWEEN ? AND ? >[["publish_year", 1975], ["publish_year", 2014]] => ["python", "typescript", "java", "c", "php", "ruby", "csharp", "kotlin", "swift"]

$ Tiobe.where(publish_year: 1975..1995).pluck(:language, :publish_year)

(2.5ms) SELECT "tiobes"."language", "tiobes"."publish_year" FROM "tiobes" WHERE "tiobes"."publish_year" BETWEEN ? AND ? [["publish_year", 1975], ["publish_year", 1995]] => [["python", 1980], ["java", 1995], ["c", 1975], ["php", 1995], ["ruby", 1995]]

works like pluck but returns an ActiveRecord::Relation.
$ Tiobe.where(publish_year: 1975..1995).select(:language)

first_or_create and first_or_initialize
The methods first_or_create and first_or_initialize are ways to search for a specific entry in your database or create one if the entry doesn’t exist already. Both have to be chained to a where search.
$ Tiobe.where(language: 'Test')
$ test = Tiobe.where(language: 'Test').first_or_create


After performing above steps you made nano app, which create model, add records via database seeding rather then manual and run sql queries . By making these nano app, you also know the basics of Active Records, basic sql commands .

Share this