How could I get the reduced result and its value using Couchbase::View?

Ruby SDK => v.1.3
Couchbase server => v.2.2

I have the following code…


function (doc, meta) { emit(, 1); }


function (key, values, rereduce) {
  // This is a built-in count reduce
  if (rereduce) {
        var result = 0;
        for (var i = 0; i < values.length; i++) {
            result += values[i];
        return result;
    } else {
        return values.length;


class MyCouchbaseModel.rb < Couchbase::Model
    view :my_view


def index
    count = MyCouchbaseModel.my_view
    puts count
    # #<Couchbase::View:0x007fa880b42c80>
    # => nil

It looks like count is nil.
How could I get the reduced result and its value?
Am I doing something wrong here?

Why don’t you just use _count in the reduce.js function? Also, you don’t need to emit as the key, since it always gets sent over in non-reduced cases (and in reduced it doesn’t matter anyways here)

when you say puts count it will print the object to STDOUT by calling to_s method on it. Here in Couchbase::View this method will output the object itself, just like you call puts count.inspect.

The nil in your example, probably the value returned by the puts which always returns nil:

if you need to access actual results, the Couchbase::View returns, you need to enumerate them with each or converting to Array

In particular, from the beer sample mentioned above

Instantiating query in controller app/controllers/countries_controller.rb

def index
  @countries = Brewery.by_country

Fetching and rendering results in view app/views/countries/index.html.erb

<% @countries.each do |country| %>
      <%= link_to country.key, country_path(country) %>
      <span class="badge pull-right"><%= country.value %></span>
<% end %>
1 Like

Actually, my code is much different and complicated. I just wrote a sample code since I could not access the actual results of reduced data.

I already have tried the followings:


MyCouchbaseModel.my_view.each do |row|
    puts row.inspect

Everything gives me TypeError: no implicit conversion of nil into String

Everything works fine on the couchbase web console.
If I do MyCouchbaseModel.my_view(:reduce => false).count, it gives me the count.

I solved the problem~!!

MyCouchbaseModel.my_view(:include_docs => false)

If I did this, everything is working fine.
Now, my question is what does :include_docs actually do?

And it looks like the API states that :include_docs is false as default.
How come my application’s :include_docs is set as true? (weird…)

Because in model context, it flips default, because it most cases you do need load document bodies

1 Like

I see.
Then, do you know why I get TypeError: no implicit conversion of nil into String when :include_docs => true?

I think that because when include_docs specified, it tries to fetch the document body using its ID, and obviously there no id attribute in the reduced results

1 Like