Skip to content
The ORM-like abstraction for couchbase ruby client (Fork for *experiments*. See original repository)
Ruby JavaScript
Find file
Latest commit 5a60ccd Nov 23, 2012 @avsej Bump version 0.5.1
Change-Id: I3c982ea9f4c5906eb2b80fabb98182b918972e98
Reviewed-by: Sergey Avseyev <>
Tested-by: Sergey Avseyev <>
Failed to load latest commit information.
lib Bump version 0.5.1 Nov 29, 2012
tasks Do not make zipball Apr 6, 2012
test Introduce save! and create! methods and raise RecordInvalid only from… Nov 26, 2012
.travis.yml Use preview repository for travis Apr 6, 2012
.yardopts Ruby gem infrastructure Feb 22, 2012
Gemfile Ruby gem infrastructure Feb 22, 2012
README.markdown Add note about validations in the README Sep 26, 2012


Couchbase Model

This library allows to declare models for couchbase gem.


If you found an issue, please file it in our JIRA. Also you are always welcome on #libcouchbase channel at IRC servers.


Rails integration

To generate config you can use rails generate couchbase:config:

$ rails generate couchbase:config
create  config/couchbase.yml

It will generate this config/couchbase.yml for you:

common: &common
  hostname: localhost
  port: 8091
  pool: default

  <<: *common
  bucket: couchbase_tinyurl_development

  <<: *common
  bucket: couchbase_tinyurl_test

# set these environment variables on your production server
  hostname: <%= ENV['COUCHBASE_HOST'] %>
  port: <%= ENV['COUCHBASE_PORT'] %>
  username: <%= ENV['COUCHBASE_USERNAME'] %>
  password: <%= ENV['COUCHBASE_PASSWORD'] %>
  pool: <%= ENV['COUCHBASE_POOL'] %>
  bucket: <%= ENV['COUCHBASE_BUCKET'] %>


require 'couchbase/model'

class Post < Couchbase::Model
  attribute :title
  attribute :body
  attribute :draft

p = => 'hello-world',
             :title => 'Hello world',
             :draft => true)
p = Post.find('hello-world')
p.body = "Once upon the times...."
p.update(:draft => false)
Post.bucket.get('hello-world')  #=> {"title"=>"Hello world", "draft"=>false,
                                #    "body"=>"Once upon the times...."}

You can also let the library generate the unique identifier for you:

p = Post.create(:title => 'How to generate ID',
                :body => 'Open up the editor...')        #=> "74f43c3116e788d09853226603000809"

There are several algorithms available. By default it use :sequential algorithm, but you can change it to more suitable one for you:

class Post < Couchbase::Model
  attribute :title
  attribute :body
  attribute :draft

  uuid_algorithm :random

You can define connection options on per model basis:

class Post < Couchbase::Model
  attribute :title
  attribute :body
  attribute :draft

  connect :port => 80, :bucket => 'blog'


There are all methods from ActiveModel::Validations accessible in context of rails application:

class Comment < Couchbase::Model
  attribute :author, :body

  validates_presence_of :author, :body

Views (aka Map/Reduce indexes)

Views are stored in models directory in subdirectory named after the model (to be precious design_document attribute of the model class). Here is an example of directory layout for Link model with three views.

└── app
    └── models
        ├── link
        │   ├── total_count
        │   │   ├── map.js
        │   │   └── reduce.js
        │   ├── by_created_at
        │   │   └── map.js
        │   └── by_view_count
        │       └── map.js
        └── link.rb

To generate view you can use yet another generator rails generate couchbase:view DESIGNDOCNAME VIEWNAME. For example how total_count view could be generated:

$ rails generate link total_count

The generated files contains useful info and links about how to write map and reduce functions, you can take a look at them in the templates directory.

In the model class you should declare accessible views:

class Post < Couchbase::Model
  attribute :title
  attribute :body
  attribute :draft
  attribute :view_count
  attribute :created_at, :default => lambda { }

  view :total_count, :by_created_at, :by_view_count

And request them later:

Post.by_created_at(:include_docs => true).each do |post|
  puts post.title

Post.by_view_count(:include_docs => true).group_by(&:view_count) do |count, posts|
  p "#{count} -> #{{|pp| pp.inspect}.join(', ')}"
Something went wrong with that request. Please try again.