Porting from ActiveRecord to CouchRest Model

Will Holcomb

1 May 2014


I have been tasked with writing a mobile app for collecting data about gas wells. My forays into the field before showed me how difficult the synchronization of complex data is, so I wanted to use CouchDB for the data store. CouchDB has a pool of javascript objects, each with a guid so they can be copied with impunity, The server keeps track of a revision history as well as conflict history when there are conflicting writes.

The rails application that the mobile app will be syncing with is currently running on Postgresql. One solution would be maintaining separate databases and using CouchDB's continuous replication to listen for changes and push them to the rails app. This duplicates data and there's no simple mechanism for listening for changes to the rails database. A better solution would be if the rails app was using the same data store — enter CouchRest Model.

The big limitation on CouchRest Model is there are no find or where methods since those rely on SQL. Also an unknown number of other gems rely on ActiveRecord as well. I'm going to chart my progress here.

Setup

Installing CouchRest Model is as simple as adding gem 'couchrest_model' to the Gemfile and running bundle install.

Tests

Fortunately, the rails developer added tests. To run just the model tests, I do:

  1. RAKE_ENV=test rake db:migrate:reset db:test:prepare
  2. rspec spec/models

And the client needs to be able to run SQL queries on the data, so nevermind…