Seeding in Rails

Joshua Chipile
3 min readNov 9, 2021

--

What is Seed in Rails ? A useful way of populating a database with the initial data needed for a Rails project. This will allow us to populate the database in within our Rails Web application.

Seeding in Rails

Prerequisite:

  • Ruby version — 3.0
  • Rails version — 6.1.4.1
  • Rails Packages
  • Configuration of the rails app (to use rbenv — Rails virtual environment)
  • Creating new Rails Web App

Create Rails Application

The first step is to create or generate a new Rails application.

$ mkdir learn
cd learn
$ learn/
rails new simple_library_seed
$ learn/
cd simple_library_seed
$ learn/simple_seed/

Model:

Time to create model for our Rail app.

$ simple_library_seed
rails g model Book title author description:text read_on:date

Migrate:

Migrate simple_library_seed application. The migration allows one to define changes to your database schema.

$ simple_library_seed
rails db:migrate
== 20211029095705 CreateBooks: migrating ======================================
-- create_table(:books)
-> 0.0022s
== 20211029095705 CreateBooks: migrated (0.0030s) =============================

Seed in Rails:

Adding to model data database using seeds.rb. Open and edit $simple_library_seed/db/seeds.rb. Enter your choice of text editor (Visual Studio Code, Sublime Text, Atom, Brackets , Gedit, Notepad++, Vim) .

Copy and Paste the code below within seeds.rb.

Book.destroy_allBook.create!([{
title: "Physics",
author: "Pete Docter",
description: "A.",
read_on: 1.week.ago
},
{
title: "Biology",
author: "Pete Docter",
description: "B.",
read_on: 2.years.ago
},
{
title: "English",
author: "Pete Doct",
description: "C.",
read_on: 3.years.ago
}])
p "Created #{Book.count} Books"

Seeding Rails

$ simple_library_seed
rails db:seed
"Created 3 Books"
rails runner 'p Book.pluck :title'

Custom rails model

Create custom Rails model for our application.

$ simple_library_seed
rails g model Subject name
Running via Spring preloader in process 18031
invoke active_record
create db/migrate/20211028123748_create_subjects.rb
create app/models/subject.rb
invoke test_unit
create test/models/subject_test.rb
create test/fixtures/subjects.yml
rails db:migrate
== 20211028123748 CreateSubjects: migrating ===================================
-- create_table(:subjects)
-> 0.0049s
== 20211028123748 CreateSubjects: migrated (0.0052s) ==========================

Using the Terminal / Command line . Using the command below.

$ simple_library_seed
rails g task books seed_subjects
Running via Spring preloader in process 18180
create lib/tasks/books.rake

Open and edit the content within books.rake, enter your choice of text editor (Visual Studio Code, Sublime Text, Atom, Brackets , Gedit, Notepad++, Vim) .$ simple_seed/lib/tasks/books.rake

namespace :books do
desc "Seeds subjects"
task seed_subjects: :environment do
Subject.create!([{
name: "test_comic_1"
},
{
name: "test_comic_2"
},
{
name: "test_comic_3"
}])
p "Created #{Subject.count} subjects"
end
end

Seeds using the console

Using the Terminal / Command line . Using the command below.

$ simple_library_seed
rails c
Running via Spring preloader in process 18908
Loading development environment (Rails 6.1.4.1)
# Rails.application.load_seed
irb(main):001:0> Rails.application.load_seed
(0.5ms) SELECT sqlite_version(*)
Book Load (0.2ms) SELECT "books".* FROM "books"
TRANSACTION (0.2ms) begin transaction
Book Destroy (0.6ms) DELETE FROM "books" WHERE "books"."id" = ? [["id", 7]]
TRANSACTION (136.7ms) commit transaction
TRANSACTION (0.1ms) begin transaction
Book Destroy (0.3ms) DELETE FROM "books" WHERE "books"."id" = ? [["id", 8]]
TRANSACTION (164.8ms) commit transaction
TRANSACTION (0.1ms) begin transaction
Book Destroy (0.3ms) DELETE FROM "books" WHERE "books"."id" = ? [["id", 9]]
TRANSACTION (190.6ms) commit transaction
TRANSACTION (0.1ms) begin transaction
Book Create (0.3ms) INSERT INTO "books" ("title", "author", "description", "read_on", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "Physics"], ["author", "Pete Docter"], ["description", "A."], ["read_on", "2021-10-21"], ["created_at", "2021-10-28 12:58:45.373940"], ["updated_at", "2021-10-28 12:58:45.373940"]]
TRANSACTION (110.3ms) commit transaction
TRANSACTION (0.1ms) begin transaction
Book Create (0.4ms) INSERT INTO "books" ("title", "author", "description", "read_on", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "Biology"], ["author", "Pete Doct"], ["description", "B."], ["read_on", "2019-10-28"], ["created_at", "2021-10-28 12:58:45.488035"], ["updated_at", "2021-10-28 12:58:45.488035"]]
TRANSACTION (115.7ms) commit transaction
TRANSACTION (0.2ms) begin transaction
Book Create (0.7ms) INSERT INTO "books" ("title", "author", "description", "read_on", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "English"], ["author", "Pete Doct"], ["description", "C."], ["read_on", "2018-10-28"], ["created_at", "2021-10-28 12:58:45.607990"], ["updated_at", "2021-10-28 12:58:45.607990"]]
TRANSACTION (109.9ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "books"
"Created 3 Books"
=> true

You clone the repo github and modify the code

The End. Happy coding :)

--

--

Joshua Chipile

Software Developers Advocate||Software Engineer /Software Developer|| Technical Write || Trainer || GDG Co-Lead || OSC Co-Lead