Person(name, dob, gender, zipcode)
Event(name, date)
Registrations(person_id, event_id, status, comment)
rails generate resource Person name:string dob:date gender:string zipcode:string
rails generate resource Event name:string event_date:date event_time:time
rails generate model Registration person_id:integer event_id:integer
Event.create(name: 'Welcome', event_date: 'Feb 1 2014', event_time: '12:00pm')
Person.create(name: 'Angelo Smith', dob: 'Jan 2, 1991', gender: 'f', zipcode: 02474)
Registration.create(person_id: 3, event_id: 2)
rake db:create # creates the database for the current env
rake db:seed # (only) runs the db/seed.rb file
rake db:setup # runs db:schema:load, db:seed
In the database console you can type classic SQL commands
rails dbconsole
.help
.databases
select * from people
select * from people where id > 1
In the rails console you can type ruby commands (it’s pry)
ActiveRecord::Base.connection.tables
Person.all
Person.where(gender: 'm')
Person.where("dob > ?", Date.new(1995))
Person.where("dob > ?", Date.new(1995)).count
p = Person.new(name: "Pito", dob: '12-12-2012', gender: 'f', zipcode: 12355)
p.new_record?
p.save
p = Person.create(name: "Amin", dob: '12-12-2014', gender: 'm', zipcode: 31231)
p.delete
class Person < ActiveRecord::Base
has_many :registrations
has_many :events, through: :registrations
end
class Event < ActiveRecord::Base
has_many :registrations
has_many :people, through: :registrations
end
class Registration < ActiveRecord::Base
belongs_to :person
belongs_to :event
end
Person.find(1).registrations
Person.where(name: "Angelo Smith").first.registrations
Event.find(1).people
Validations are applied by ActiveRecord, NOT the database
validates :zipcode, length: { is: 5} # declared in model file
p = Person.find(1)
p.zipcode = 1 # assign invalid value
p.valid? # explicit check
p.save # Is not permitted into the database
p.save! # Same but throws exception
gem 'pry-rails'
gem 'pry'
gem 'pry-debugger'
gem 'pry-stack_explorer'
gem 'awesome_print'
gem 'hirb'