Hands-on with Rails DBs and Models

Live Coding

Database schema for an event schedule
1Person(name, dob, gender, zipcode)
2Event(name, date)
3Registrations(person_id, event_id, status, comment)
Create rails app
1rails new handson
Gemfile
1gem 'pry-rails'
2gem 'better_errors'
3gem 'awesome_print'
4gem 'faker'
Create the database tables
1rails generate resource Person name:string dob:date gender:string zipcode:string
2rails generate resource Event name:string event_date:date event_time:time
3rails generate model Registration person_id:integer event_id:integer
Define sample data in seeds.rb
 1Event.delete_all
 2Person.delete_all
 3Registration.delete_all
 4
 55.times do
 6  event_name = (Faker::Hacker.ingverb + " " + Faker::Hacker.noun + "s").capitalize
 7  Event.create(name: event_name, event_date: Faker::Date.between(1.year.ago, 1.day.ago), event_time: '12:00pm')
 8end
 9
10(1..10).each do
11  p = Person.create(name: Faker::Name.name,
12    dob: Faker::Date.between(30.years.ago, 10.years.ago),
13    gender: ['f', 'm'].sample, zipcode: Faker::Address.zip)
14end
15
163.times do
17  r = Registration.create(person_id: Person.all.sample.id, event_id: Event.all.sample.id)
18end
Commands relating to seeding
1rails db:create  # creates the database for the current env
2rails db:migrate # Run latest migrations
3rails db:seed    # (only) runs the db/seed.rb file
4rails db:setup   # runs db:schema:load, db:seed
Examining the DB from the SQL console

In the database console you can type classic SQL commands

1rails dbconsole
2.help
3.databases
4.tables
5select * from people;
6select * from people where id > 1;
Examining the databases from the Rails console

In the rails console you can type ruby commands (it’s pry)

1rails console
2ActiveRecord::Base.connection.tables
3Person.all
4Person.where(gender: 'm')
5Person.where("dob > ?", Date.new(1995))
6Person.where("dob > ?", Date.new(1995)).count
CRUD operations (Create, Read, Update, Delete)
1p = Person.new(name: "Pito", dob: '12-12-2012', gender: 'f', zipcode: 12355)
2p.new_record?
3p.save
4p = Person.create(name: "Amin", dob: '12-12-2014', gender: 'm', zipcode: 31231)
5p.delete
Associations, declared in models
 1class Person < ActiveRecord::Base
 2  has_many :registrations
 3  has_many :events, through: :registrations
 4end
 5class Event < ActiveRecord::Base
 6  has_many :registrations
 7  has_many :people, through: :registrations
 8end
 9class Registration < ActiveRecord::Base
10  belongs_to :person
11  belongs_to :event
12end
Association, used
1Person.find(1).registrations
2Person.where(name: "Angelo Smith").first.registrations
3Event.find(1).people
Validations

Validations are applied by ActiveRecord, NOT the database

1validates :zipcode, length: { is: 5}    # declared in model
2
3p = Person.find(1)
4p.zipcode = 1                           # invalid value
5p.valid?                                # explicit check
6p.save                                  # Is not permitted
7p.save!                                 # Same but exception

Appendices

References