Mark Eschbach

Software Developer && System Analyst

CouchRest Model

CouchRest Model promises to be a near drop-in replacement for ActiveRecord.

Only updating views when requested (2016-01)

Getting CouchRest Model installed and gerating proper models worked well. However as a general rule I don't like systems autoupdating the views nor creating database themselves. In order to lock down view management you need to set the property auto_update_design_doc to false. The easiest method to do so application wide is to use config/application.rb calling CouchRest::Model::Base.configure and providing a block on which to set the property.

The second part of this is to update the actual models. This can be done either in the console or through an environmnet file as descrbed in Guide. Unforunately their example didn't work for me as CouchRest::Model::Base.subclasses always appears in the rails console for me. I have modified my config/couchdb.yml to take environment variables (COUCH_USER and COUCH_PASS) or use a default. For example:

  1. COUCH_USER="demo-user" COUCH_PASS="demo-pass" rails console
  2. unning via Spring preloader in process 39399
    Loading development environment (Rails 4.2.5)
    irb(main):001:0> User.design_doc.sync!
    => #<CouchRest::Model::Design _id: "_design/User", language: "javascript", views: {"by_email"=>{"map"=>"                function(doc) {\n                  if ((doc['type'] == 'User') && (doc['email'] != null)) {\n                    emit(doc['email'], 1);\n                  }\n                }\n", "reduce"=>"_sum"}, "all"=>{"map"=>"                function(doc) {\n                  if (doc['type'] == 'User') {\n                    emit(doc._id, null);\n                  }\n                }\n"}}, couchrest-hash: "3a1834b9be06e7842a494c12e7943a7c">
    irb(main):002:0>
    		
  3. Verify the design document exists within your database. If now error was returned you should be fine.