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. [code language=”ruby”] 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 [/code] Don’t forget that you’ll need to install the gems using bundler. [code language=”bash”] bundle install [/code] And …

Read more

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: [code language=”ruby”] gem ‘annotate’ [/code] Then run “bundle install” from the command line. Step 2 Generate the schema annotations by running: [code language=”bash”] annotate [/code] 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: [code language=”ruby”] # == Schema Information # # Table name: users # # id :integer not null, primary key # screen_name :string(255) # name :string(255) # created_at :datetime # updated_at :datetime # [/code]