The file app.rb
is a Sinatra app, really simple. But what if you want to interact with your database without Sinatra? Ruby has irb
a program that lets you run ruby commands one line at a time. To start using irb
open up Terminal and cd
to the folder your app is in. Then type irb
and hit enter. If you are trying to connect to Heroku use heroku run irb
WARNING: changing data on Heroku will change it for your web app, so be sure you know what you are doing.
You'll notice the prompt changes to an angle bracket (>) from a dollar sign ($) or something.
First we need to require the ruby file containing our app:
> require './app'
We now have access to the code in the app. If you type
> shape = Shape.new
You'll get: => #<Shape @id=nil @name=nil @sides=nil @regular=nil>
You've just made a new Shape object, it hasn't been saved to the database yet, let's give it some info and then save it to the database.
> shape.name = "square"
# => "square"
> shape.sides = 4
# => 4
> shape.regular = true
# => true
> shape.save
# => true
Now that we have a shape, let's get all of the shapes:
shapes = Shape.all
For further documentation check out http://datamapper.org/docs/