# initialize from scratch rails new demo && cd demo # commit initial version (so changes are highlighted) git add . git commit -m "first commit" rails generate model Student fname:string lname:string buckid:integer # see student.rb, xxxx_create_students.rb rails console > Student.methods > Student.all # will this work? > s = Student.new > exit rails db:migrate # creates schema.rb rails console > Student.all > Student.all.size > s1 = Student.new > s2 = Student.new fname: 'Jo' > s3 = Student.new fname: 'Primo', lname: 'Carnera' > s2 # what is s2.buckid? > Student.all # how many students? > s2.save > s3.save > Student.all # now how many students? > t = Student.find 1 # which one is found? > t = Student.find_by fname: 'Primo' > t = Student.first ###################### # Optional: seeding the database bundle add faker # db/seeds.rb 30.times do Student.create!( buckid: Faker::Number.unique.number(digits: 9), fname: Faker::Name.first_name, lname: Faker::Name.last_name) end # update database (development.sqlite3) rails db:seed # run seeds.rb rails db:reset # for a fresh start rails c > Student.all.size > Student.last ##################### # Add a table (:teams) an association with :students rails generate model Team name:string rails generate migration AddTeamToStudents team:references rails db:drop # clear out the old database rails db:migrate # student.rb class Student < ApplicationRecord belongs_to :team # note singular form # adds Student#team method end # team.rb class Team < ApplicationRecord has_many :students # note plural form # adds Team#students method end #################### rails c > Student.all # no students > Student.create fname: 'Jo' # why does this fail? > t = Team.create name: 'Cloud Nine' > Student.create fname: 'Primo', team_id: 1 # set team_id directly > Student.create lname: 'Cher', team: t # alternatively use team object > t.students.create fname: 'Marco', lname: 'Pantani' # alternative > Student.all > Student.first.team > Team.first.students ##################### # Improve faker data to make teams too # db/seeds.rb 6.times do team = Team.create(name: Faker::Team.name) 5.times do team.students.create!(buckid: Faker::Number.unique.number(digits: 9), fname: Faker::Name.first_name, lname: Faker::Name.last_name) end end # update database (development.sqlite3) rails db:seed # run seeds.rb rails db:reset # for a fresh start rails c > Student.all > Team.all > Student.first.team > Team.first.students ##################### # Add validations # models/student.rb class Student < ApplicationRecord belongs_to :team validates :buckid, presence: true, uniqueness: true validates :lname, presence: true end # models/team.rb class Team < ApplicationRecord has_many :students validates :name, presence: true, uniqueness: true, length: { minimum: 5 }, format: { with: /\A[a-zA-Z ]+\z/, message: "only allows letters" } end rails c > t = Teams.first > t.name = t.name + '1' # append invalid character > t.save > t.valid? > t.errors > t.errors.size > t.errors.first > t.errors.first.message > t.name = "test" # too short > t.valid? > t.name = "tester" > t.valid?