Posts Tagged ‘activerecord’
Using the Heroku Shared Database with Sinatra and Active Record
ActiveRecord is an amazing (mostly) database-agnostic ORM framework and so it’s a natural choice to use with non-Rails frameworks such as Sinatra. Note that I’ll be using sqlite3 locally but the Heroku Shared Database is a Postgres database so I’ll be setting my environments appropriately.
In this post I’ve assumed that you have a Sinatra app that is working locally and on Heroku.
Getting it working locally
First up you’ll need a few extra gems in your Gemfile, once again note that I’m using different databases in development, test and production environments.
source 'http://rubygems.org' gem 'sinatra' gem 'activerecord' gem 'sinatra-activerecord' # excellent gem that ports ActiveRecord for Sinatra group :development, :test do gem 'sqlite3' end group :production do gem 'pg' # this gem is required to use postgres on Heroku end
Don’t forget that you’ll need to install the gems using bundler.
bundle install
And you will need to “require” the appropriate files in either your app configuration or the main app controller, the choice is yours 🙂
require 'sinatra/activerecord'
At this point you need to provide information that tells your app how to connect to the database.
configure :development, :test do set :database, 'sqlite://development.db' end configure :production do # Database connection db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb') ActiveRecord::Base.establish_connection( :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, :host => db.host, :username => db.user, :password => db.password, :database => db.path[1..-1], :encoding => 'utf8' ) end
You can include this information in your Sinatra app file but I suggest putting the information into a separate configuration file. I keep mine in a file ‘/config/environments.rb’. If you do this you’ll have to include it in your Sinatra app file(s).
require './config/environments'
In order to use migrations (to set up your object model) you’ll need to create a Rakefile with the following code.
# require your app file first require './app' require 'sinatra/activerecord/rake'
At this point you can use the typical ActiveRecord Migration syntax to create you migration files, for example:
rake db:create_migration NAME=create_foos
This creates a migration file in ‘./db/migrate’ and this file will be used to create your database table on migration. You will also need to create a class that is the “bridge” between your app and the database table.
class CreateFoos < ActiveRecord::Migration def self.up create_table :foos do |t| t.string :name end end def self.down drop_table :foos end end
As with the database environment details this code can be included in your main app class but you should put it into it’s own file and include that in your app instead. Once you’ve done this you can run the following to create the database tables – this is only a local operation for now.
rake db:migrate
At this point you should have a local table and method to apply any CRUD action to said table.
And now for Heroku
Before pushing your new app to heroku you’ll need to add the Shared Database addon.
heroku addons:add shared-database
Commit and push your code to Heroku after which you’ll need to rake the remote database.
heroku rake db:migrate
And that’s it. You now have a ActiveRecord working locally and remotely and can develop in a consist way. Aw yeah.
A gem to help you document your Rails ActiveRecord model
I’ve been using the “annotate” gem for a while and it’s simple whilst incredibly useful. Essentially once run it documents each resource in your database within the appropriate file. Here’s how to get it working.
Step 1
Install the gem. There are a few ways to do this but I usually include the following line in my Gemfile:
gem 'annotate'
Then run “bundle install” from the command line.
Step 2
Generate the schema annotations by running:
annotate
in the root of your rails project directory and it will inject the schema details for each “table” into the respective “model.rb” file e.g. “user.rb” might contain:
# == Schema Information # # Table name: users # # id :integer not null, primary key # screen_name :string(255) # name :string(255) # created_at :datetime # updated_at :datetime #