Up and Running with Sinatra and ActiveRecord
> mkdir activerecord-sinatra && cd activerecord-sinatra > touch app.rb
Install two gems for ActiveRecord and the JSON gem for a real API-looking output.
> gem install activerecord sinatra-activerecord json
I will be using Postgres on my local machine, so my create commands are as follows:
> createdb -Omatt -Eutf8 activerecord_sinatra
Now open the “app.rb” file and edit it as so.
#app.rb require 'sinatra' require 'sinatra/activerecord' require 'json' set :database, 'postgres://matt@localhost/activerecord_sinatra' class Course < ActiveRecord::Base belongs_to :teacher end class Teacher < ActiveRecord::Base has_many :courses end get '/' do "Hello World!" end get '/courses' do Course.all.to_json end get '/course/:id' do Course.find(:first, params[:id]).to_json end get '/teachers' do Teacher.all.to_json end get '/teacher/:id' do Teacher.find(:first, params[:id]).to_json end #show which courses the teacher teaches get '/teacher/:id/courses' do Teacher.find(params[:id]).courses.to_json end
ActiveRecord migrations are powerful, because they create a schema-independent way to control databases. Rake requires a rakefile to be present, so we will create that.
> touch Rakefile
Open the Rakefile and add in the following require statements.
#Rakefile require './app' require 'sinatra/activerecord/rake'
Now you are able to create and manage migrations, much like you can with Ruby on Rails.
> rake db:create_migration NAME=create_courses_and_teachers
This will create a directory tree in your folder of the pattern “/db/migrate/{migration}”. Open the file and modify it as follows.
class CreateCoursesAndTeachers < ActiveRecord::Migration def self.up create_table :courses do |t| t.string :title t.integer :teacher_id end create_table :teachers do |t| t.string :name end end def self.down drop_table :courses drop_table :teachers end end
Run the migration.
> rake db:migrate
For ease of use, you can add the following to the “app.rb” file to autogenerate some data.
#put below everything in app.rb, only run once! get '/make' do teacher1 = Teacher.create(:name => "Mrs. Crabtree") teacher2 = Teacher.create(:name => "Alberto Yeinstein") Course.create(:title => "Algebra 2", :teacher_id => teacher1.id) Course.create(:title => "Basic Geometry", :teacher_id => teacher1.id) Course.create(:title => "Applied Physics", :teacher_id => teacher2.id) Course.create(:title => "General Relativity", :teacher_id => teacher2.id) Course.create(:title => "Complex Geometrical Spaces", :teacher_id => teacher2.id) end
Now, you can run the program and data should appear at each one of the locations (“API endpoints”) that you created.
> ruby app.rb
You can see the full project that was explained above on Github, check it out.
Recent Comments
Archives
- April 2023
- January 2023
- November 2022
- May 2022
- March 2022
- January 2022
- December 2021
- April 2021
- December 2020
- October 2020
- August 2020
- July 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- January 2019
- December 2018
- November 2018
- August 2018
- July 2018
- April 2018
- March 2018
- November 2017
- October 2017
- February 2017
- October 2016
- August 2016
- July 2016
- November 2015
- October 2013
- February 2013
- January 2013
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- February 2012
- December 2011