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]

“Dropping” a mongohq database on Heroku

I’ve used inverted commas around the word ‘dropping’ because it doesn’t look like you can drop a mongohq database – what you can do however is drop all the collections in the database. I was doing this through the add-on interface in heroku but that got tired quickly, and after some googling it looked like the solution was to write a customĀ rake task and drop it into lib/tasks/; the file can be named anything with a ‘.rb’ extension. The code follows: [code language=”ruby”] namespace :mdb do desc ‘Drops all the collections for the database for the current Rails.env’ task :drop => :environment do if ENV[‘MONGOHQ_URL’] uri = URI.parse(ENV[‘MONGOHQ_URL’]) conn = Mongo::Connection.from_uri(ENV[‘MONGOHQ_URL’]) DB = conn.db(uri.path.gsub(/^\//, ”)) DB.collections.each do |collection| begin collection.drop rescue puts "Can’t drop: " + collection.name end end end end end [/code] Note that the exception handler isn’t perfect and essentially all I’m doing is skipping the system collections …

Read more

Ruby, Rails & RVM woes

I’ve had several issues over the past day pertaining to Ruby, Rails and RVM and all of them were caused by one silly thing. Problem 1: You can’t swap between Ruby versions using RVM If you can’t swap between Ruby versions that you know you’ve installed then you’ve probably installed something using “sudo”. It is highly recommended that you install & use RVM in the single user mode as this is easiest. If you do this then all the versions of Ruby etc. that you install will be installed in your home directory (usually ~/.rvm/) instead of system wide and this negates a whole bunch of complexities. Running this command will tell you which ruby executable you’re using: [code language=”bash”] which ruby [/code] If the executable is not sitting in your home directory e.g. it’s in /usr/local/bin then you’re in a spot of bother. The easiest way I’ve found to …

Read more