This is a minimalist implementation of ActiveRecord with one Restaurant
model to live-code a demo for @LeWagon students, part of Karr examples, working with Sinatra.
- clone the repo
- run
rake db:create
to create your db - run your migrations with
rake db:migrate
Now you can play with the Restaurant
model:
rake console
Here are some commands you can run in the console:
restaurant = Restaurant.new(name: "La Tour d'Argent", address: "15 Quai de la Tournelle, 75005 Paris")
restaurant.persisted?
# => false
restaurant.id
# => nil
restaurant.save
restaurant.id
# => 1
To launch a Sinatra server just run this and open a web browser at http://localhost:4567:
bundle exec ruby app.rb
Enjoy!
- Restaurant.all
- Restaurant.find(1)
- Restaurant.find_by(name: 'The Leaky Cauldron') # returns one
- hogs_head = Restaurant.new(name: "Hog's Head", address: 'Hogsmeade')
- hogs_head.save
- Restaurant.create(name: "Hog's Head", address: 'Hogsmeade')
- update_restaurant = Restaurant.find(1)
- update_restaurant.address = 'London'
- update_restaurant.save
- destroy_restaurant = Restaurant.find(1)
- destroy_restaurant.destroy
- Restaurant.where(address: 'London') # returns multiple