Skip to content

Instantly share code, notes, and snippets.

@davidjrice
Created June 14, 2012 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjrice/2931928 to your computer and use it in GitHub Desktop.
Save davidjrice/2931928 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
before_filter :fetch_authors_and_categories
#...
private
def fetch_authors_and_categories
Couch.client(:bucket => 'system').pipelined do
@authors = Author.popular
@categories = Category.popular
end
end
#...
end
class Author < Couchbase::Model
#...
def self.popular(limit=8)
begin
contribs = Couch.client(:bucket => 'system').get("contributors")["data"]
return contribs.map! { |contrib| new (contrib) }
rescue Couchbase::Error::NotFound
#...
return []
end
end
end
module Couch
class << self
def url
ENV['COUCHBASE_URL'] || "http://127.0.0.1:8091/pools/default"
end
def client(options = {})
bucket = options.delete(:bucket) || "default"
@clients ||= {}
@clients[bucket] ||= Couchbase.new(url, :bucket => bucket)
end
end
end
@avsej
Copy link

avsej commented Jun 14, 2012

Couch.client(:bucket => 'system').run do
  Author.popular.load {|res| @authors = res.collection}
  Category.popular.load {|res| @categories = res.collection}
end

Where #load is the same as #fetch but yield whole collection at once

@avsej
Copy link

avsej commented Jun 14, 2012

It should work right now with

@articles = []
Couch.client(:bucket => 'system').run do
  Article.popular.fetch{|doc| @articles << doc}
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment