DevNation
DevNation

DevNation

Rails ORM-2

Rails ORM-2

DevNation's photo
DevNation

Published on Jul 17, 2021

3 min read

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.

Task:-

You will create model, add records via seeds.rb and perform new_ record?, which stored in sqlite db.

software requirments:-

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

Level:-

Beginner

Prerequisite:-

Knowledge of ruby language and text editor

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. $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-

Populating the Database with seeds.rb

With seeds.rb, you don’t need to enter everything manually with rails console to create all the initial records in a new Rails application.You have full access to all classes and methods of your application.

Tiobe.create(language: 'csharp', rank: 7)

Tiobe.create(language: 'swift', rank: 8)

Tiobe.create(language: 'kotlin', rank: 9)

$ rails db:seed

Database Configuration Let’s take a quick look at the configuration file for the database (config/database.yml), as shown SQLite. Versions 3.8.0 and up are supported. gem 'activerecord-jdbcsqlite3-adapter'

Configure Using Gemfile gem 'activerecord-jdbcsqlite3-adapter'

default: &default adapter: sqlite3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development: <<: *default database: db/development.sqlite3

Warning: The database defined as "test" will be erased and re-generated from your development database when you run "rake". Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3

production: <<: *default database: db/production.sqlite3 You can specify all the database connection details in config/database.yml file.

Step3-

new_record?

With the method new_record?, you can find out whether a record has already been saved. If a new object has been created with new and has not yet been saved, then the result of new_record? is true. After a save, it’s false.

$ vb = Tiobe.new(language: 'VB')

$ vb.new_record?

$ vb.save

Conclusion:-

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

 
Share this