Doing the simple stuff so you don't have to
Ruby JavaScript
Pull request Compare This branch is 350 commits behind couchrest:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

CouchRest Model: CouchDB, close to shiny metal with rounded edges

CouchRest Models adds additional functionality to the standard CouchRest Document class such as setting properties, callbacks, typecasting, and validations.

Originally called ExtendedDocument, the new Model structure uses ActiveModel, part of Rails 3, for validations and callbacks.

If your project is still running Rails 2.3, you'll have to continue using ExtendedDocument as it is not possible to load ActiveModel into programs that do not use ActiveSupport 3.0.

CouchRest Model is only tested on CouchDB 1.0.0 or newer.



$ sudo gem install couchrest_model


If you're using bundler, just define a line similar to the following in your project's Gemfile:

gem 'couchrest_model'

You might also consider using the latest git repository. All tests should pass in the master code branch but no guarantees!

gem 'couchrest_model', :git => 'git://'


CouchRest Model now comes with a Gemfile to help with development. If you want to make changes to the code, download a copy then run:

bundle install

That should set everything up for rake spec to be run correctly. Update the couchrest_model.gemspec if your alterations use different gems.



    $ rails generate model person --orm=couchrest_model

Useful links and extensions

Try some of these gems that add extra funcionality to couchrest_model:

  • memories - object versioning using attachments (Matt Parker)
  • couch_publish - versioned state machine for draft and published documents (Matt Parker)
  • couch_photo - attach images to documents with variations (Matt Parker)
  • copycouch - single document replication on documents (Matt Parker)
  • recloner - clone documents easily (Matt Parker)

If you have an extension that you'd us to add to this list, please get in touch!

General Usage

require 'couchrest_model'

class Cat < CouchRest::Model::Base

  property :name,      String
  property :lives,     Integer, :default => 9

  property :nicknames, [String]


  view_by :name


@cat = => 'Felix', :nicknames => ['so cute', 'sweet kitty'])   # true

@cat['name']   # "Felix"

@cat.nicknames << 'getoffdamntable'

@cat =
@cat.update_attributes(:name => 'Felix', :random_text => 'feline') # false
@cat.random_text  # Raises error!


A property is the definition of an attribute, it describes what the attribute is called, how it should be type casted and other options such as the default value. These replace your typical add_column methods found in relational database migrations.

Attributes with a property definition will have setter and getter methods defined for them. Any other attibute can be set in the same way you'd update a Hash, this funcionality is inherited from CouchRest Documents.

Here are a few examples of the way properties are used:

class Cat < CouchRest::Model::Base
  property :name
  property :birthday

@cat = => 'Felix', :birthday => 2.years.ago)        # 'Felix'
@cat.birthday.is_a?(Time)  # True!
@cat = Cat.find(        # 'Felix'
@cat.birthday.is_a?(Time)  # False!

Properties create getters and setters similar to the following:

def name

def name=(value)
  write_attribute('name', value)

Properties can also have a type which will be used for casting data retrieved from CouchDB when the attribute is set:

class Cat < CouchRest::Model::Base
  property :name, String
  property :last_fed_at, Time

@cat = => 'Felix', :last_fed_at => 10.minutes.ago)
@cat.last_fed_at.is_a?(Time)   # True!
@cat = Cat.find(
@cat.last_fed_at < 20.minutes.ago   # True!

Boolean or TrueClass types will create a getter with question mark at the end:

class Cat < CouchRest::Model::Base
  property :awake, TrueClass, :default => true

@cat.awake?   # true

Adding the +:default+ option will ensure the attribute always has a value.

A read-only property will only have a getter method, and its value is set when the document is read from the database. You can however update a read-only attribute using the write_attribute method:

class Cat < CouchRest::Model::Base
  property :name, String
  property :lives, Integer, :default => 9, :readonly => true  

  def fall_off_balcony!
    write_attribute(:lives, lives - 1)

@cat = => "Felix")
@cat.lives    # Now 8!

Mass assigning attributes is also possible in a similar fashion to ActiveRecord:

@cat.attributes = { :name => "Felix" }

Is the same as:

@cat.update_attributes(:name => "Felix")

By default, attributes without a property will not be updated via the #attributes= method. This provents useless data being passed to database, for example from an HTML form. However, if you would like truely dynamic attributes, the mass_assign_any_attribute configuration option when set to true will store everything you put into the Base#attributes= method.

Property Arrays

An attribute may contain an array of data. CouchRest Model handles this, along with casting, by defining the class of the child attributes inside an Array:

class Cat < CouchRest::Model::Base
  property :name, String
  property :nicknames, [String]

By default, the array will be ready to use from the moment the object as been instantiated:

@cat = => 'Fluffy')
@cat.nicknames << 'Buffy'

@cat.nicknames == ['Buffy']

When anything other than a string is set as the class of a property, the array will be converted into special wrapper called a CastedArray. If the child objects respond to the casted_by method (such as those created with CastedModel, below) it will contain a reference to the parent.

Casted Models

CouchRest Model allows you to take full advantage of CouchDB's ability to store complex documents and retrieve them using the CastedModel module. Simply include the module in a Hash (or other model that responds to the [] and []= methods) and set any properties you'd like to use. For example:

class CatToy < Hash
  include CouchRest::Model::CastedModel

  property :name, String
  property :purchased, Date

class Cat < CouchRest::Model::Base
  property :name, String
  property :toys, [CatToy]

@cat = => 'Felix', :toys => [{:name => 'mouse', :purchases => 1.month.ago}]) == CatToy == 'mouse'

Any hashes sent to the property will automatically be converted: << {:name => 'catnip ball'} # True!

To use your own classes they must be defined before the parent uses them otherwise Ruby will bring up a missing constant error. To avoid this, or if you have a really simple array of data you'd like to model, CouchRest Model supports creating anonymous classes:

class Cat < CouchRest::Model::Base
  property :name, String

  property :toys do |toy| :name, String :rating, Integer

@cat = => 'Felix', :toys => [{:name => 'mouse', :rating => 3}, {:name => 'catnip ball', :rating => 5}]) == 5 == 'catnip ball'

Anonymous classes will only create arrays of objects.


Two types at the moment:

belongs_to :person

collection_of :tags

This is a somewhat controvesial feature of CouchRest Model that some document database purists may fringe at. CouchDB does not yet povide many features to support relationships between documents but the fact of that matter is that its a very useful paradigm for modelling data systems.

In the near future we hope to add support for a has_many relationship that takes of the Linked Documents feature that arrived in CouchDB 0.11.

Belongs To

Creates a property in the document with _id added to the end of the name of the foreign model with getter and setter methods to access the model.


class Cat < CouchRest::Model::Base
  belongs_to :mother
  property :name

kitty = => "Felix")
kitty.mother = Mother.find_by_name('Sophie')

Providing a object to the setter, mother in the example will automagically update the mother_id attribute. Retrieving the data later is just as expected:

kitty = Cat.find_by_name "Felix" == 'Sophie'

Belongs_to accepts a few options to add a bit more felxibility:

  • :class_name - the camel case string name of the class used to load the model.
  • :foreign_key - the name of the property to use instead of the attribute name with _id on the end.
  • :proxy - a string that when evaluated provides a proxy model that responds to #get.

The last option, :proxy is a feature currently in testing that allows objects to be loaded from a proxy class, such as ClassProxy. For example:

class Invoice < CouchRest::Model::Base
  attr_accessor :company
  belongs_to :project, :proxy => ''

A project instance in this scenario would need to be loaded by calling #get(project_id) on in the scope of an instance of the Invoice. We hope to document and work on this powerful feature in the near future.

Collection Of

A collection_of relationship is much like belongs_to except that rather than just one foreign key, an array of foreign keys can be stored. This is one of the great features of a document database. This relationship uses a proxy object to automatically update two arrays; one containing the objects being used, and a second with the foreign keys used to the find them.

The best example of this in use is with Labels:

class Invoice < CouchRest::Model::Base
  collection_of :labels

invoice =
invoice.labels << Label.get('xyz')
invoice.labels << Label.get('abc'){|l|} # produces ['xyz', 'abc']

See the belongs_to relationship for the options that can be used. Note that this isn't especially efficient, a get is performed for each model in the array. As with a has_many relationship, we hope to be able to take advantage of the Linked Documents feature to avoid multiple requests.


CouchRest Model automatically includes the new ActiveModel validations, so they should work just as the traditional Rails validations. For more details, please see the ActiveModel::Validations documentation.

CouchRest Model adds the possibility to check the uniqueness of attributes using the validates_uniqueness_of class method, for example:

class Person < CouchRest::Model::Base
  property :title, String

  validates_uniqueness_of :title

The uniqueness validation creates a new view for the attribute or uses one that already exists. You can specify a different view using the :view option, useful for when the unique_id is specified and you'd like to avoid the typical RestClient Conflict error:

unique_id :code
validates_uniqueness_of :code, :view => 'all'

Given that the uniqueness check performs a request to the database, it is also possible to include a :proxy parameter. This allows you to call a method on the document and provide an alternate proxy object.


# Same as not including proxy:
validates_uniqueness_of :title, :proxy => 'class'

# Person#company.people provides a proxy object for people
validates_uniqueness_of :title, :proxy => 'company.people'

A really interesting use of :proxy and :view together could be where you'd like to ensure the ID is unique between several types of document. For example:

class Product < CouchRest::Model::Base
  property :code

  validates_uniqueness_of :code, :view => 'by_product_code'

  view_by :product_code, :map => "
    function(doc) {
      if (doc['couchrest-type'] == 'Product' || doc['couchrest-type'] == 'Project') {

class Project < CouchRest::Model::Base
  property :code

  validates_uniqueness_of :code, :view => 'by_product_code', :proxy => 'Product'

Pretty cool!


CouchRest Model supports a few configuration options. These can be set either for the whole Model code base or for a specific model of your chosing. To configure globally, provide something similar to the following in your projects loading code:

CouchRest::Model::Base.configure do |config|
  config.mass_assign_any_attribute = true
  config.model_type_key = 'couchrest-type'

To set for a specific model:

class Cat < CouchRest::Model::Base mass_assign_any_attribute true end

Options currently avilable are:

  • mass_assign_any_attribute - false by default, when true any attribute may be updated via the update_attributes or attributes= methods.
  • model_type_key - 'couchrest-type' by default, is the name of property that holds the class name of each CouchRest Model.

Notable Issues

None at the moment...

Ruby on Rails

CouchRest Model is compatible with rails and provides some ActiveRecord-like methods.

The CouchRest companion rails project is great for providing default connection details for your database. At the time of writting however it does not provide explicit support for CouchRest Model.

CouchRest Model and the original CouchRest ExtendedDocument do not share the same namespace, as such you should not have any problems using them both at the same time. This might help with migrations.


The most complete documentation is the spec/ directory. To validate your CouchRest install, from the project root directory run rake, or autotest (requires RSpec and optionally ZenTest for autotest support).



Check the wiki for documentation and examples


Please post bugs, suggestions and patches to the bug tracker at

Follow us on Twitter:

Also, check