0

I have a class like this

class Todo

  TAG_REGEX = /(?:^|\s)#(\w+)/i

  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::FullTextSearch

  field :desc, type: String
  field :done, type: Boolean, default: false
  field :tags, type: Array

end

which I'm trying to map/reduce tags like this

map = %Q{
  function() {
    this.tags.forEach(function(tag){
      emit(tag, { count: 1 });
    }
  }
}

reduce = %Q{
  function(key, values) {
    var result = { count: 0 };
    values.forEach(function(value) {
      result.count += value.count;
    });
    return result;
  }
}

@map = Todo .map_reduce(map, reduce).out(replace: "tags")

When I try to iterate over the results, I see this exception

The operation: #<Moped::Protocol::Command
  @length=480
  @request_id=316
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="todozen_development.$cmd"
  @skip=0
  @limit=-1
  @selector={:mapreduce=>"todos", :map=>"\n      function() {\n        this.tags.forEach(function(tag){\n          emit(tag, { count: 1 });\n        }\n      }\n    ", :reduce=>"\n      function(key, values) {\n        var result = { count: 0 };\n        values.forEach(function(value) {\n          result.count += value.count;\n        });\n        return result;\n      }\n    ", :query=>{}, :sort=>{"created_at"=>-1}, :out=>{:replace=>"tags"}}
  @fields=nil>
failed with error "ns doesn't exist"

What am I missing here?
Anyone need more information?
Thanks

Luiz E.
  • 6,769
  • 10
  • 58
  • 98
  • I saw that Todo class is embedded in User class...so I tried this `@tags = current_user.todos.where(:done => false).map_reduce(map,reduce).out(replace: "tags")` But this didn't worked too I got `undefined method 'map_reduce' for #` – Luiz E. Oct 20 '12 at 16:34

1 Answers1

2

Todois an embedded class...There is no way to map/reduce an embedded class...

Luiz E.
  • 6,769
  • 10
  • 58
  • 98