Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library Ruby 1.0
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
2 Couchbase and Rails Tutorial
Chapter Sections
Chapters

2.3. Not your mother's ActiveRecord

2.3.1. Connecting to Couchbase
2.3.2. Defining an API
2.3.3. Defining a Couchbase Model

Rails is based on conventions, one of those is that you'll want to be using a SQL database. We don't. So what things should we do differently.

Listing 1: Gemfile

source 'http://rubygems.org'

# Rails 3.1, Asset Pipeline and Javascript
gem 'rails', '3.1.0'
group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end
gem 'jquery-rails'

# Required to give an executable JS environment
# in Production on Heroku
group :production do
  gem 'therubyracer-heroku', '0.8.1.pre3'
end

# Development / Test only.
group :development, :test do
  gem 'ruby-debug'
end
group :test do
  gem 'turn', :require => false
end

# Squish Application Dependencies
gem "couchbase", 1.0.0
gem "validate_url"

The major difference from this Gemfile and a freshly generated Rails application is that we're not using sqlite and we have required couchbase instead.

Listing 2: config/application.rb

require File.expand_path('../boot', __FILE__)

  # require 'rails/all'
  #
  # require "active_record/railtie"
  require "action_controller/railtie"
  # require "action_mailer/railtie"
  # require "active_resource/railtie"
  # require "rails/test_unit/railtie"

  if defined?(Bundler)
    # If you precompile assets before deploying to production, use this line
    # Bundler.require *Rails.groups(:assets => %w(development test))
    # If you want your assets lazily compiled in production, use this line
    Bundler.require(:default, :assets, Rails.env)
  end

  module CouchbaseTinyurl
    class Application  Rails::Application
      # ...
      config.encoding = "utf-8"

      # Configure sensitive parameters which will be filtered from the log file.
      config.filter_parameters += [:password]

      # Enable the asset pipeline
      config.assets.enabled = true
    end
  end

Out of the box, Rails will require rails/all which will load all aspects of Rails. For this application we only need action_controller. This cuts the boot time down significantly.

Listing 3: config/environments/development.rb

CouchbaseTinyurl::Application.configure do
  # ...
  # config.action_mailer.raise_delivery_errors = false
  # ...
end

One last thing to ensure unloading all those other Rails modules doesn't hurt us. We need to remove the action_mailer configuration options for Development and Test environments.

Now that we've got our application configured just right for using Couchbase we can start connecting with the server. You would connect to the address of the server (in this case it is 127.0.0.1 which is the IPV4 address of localhost.