Ruby mongodb map reduce

Hello,

i have this query:

db.minterviews.group({
“key”: {
“canal”: true
},
“initial”: {
“contact”: 0,
“sent”: 0,
“answer”: 0,
“repondant”: 0
},
“reduce”: function(obj, prev) {
if (true != null) if (true instanceof Array) prev.contact +=
true.length;
else prev.contact++;
prev.sent = prev.sent + obj.count_sent - 0;
prev.answer = prev.answer + obj.count_answers - 0;
prev.repondant = prev.repondant + obj.is_finished - 0;
},
“cond”: {

    "campaign_id": 88,
    "created_at": {
       $gte: ISODate("2013-01-01 00:00:00"),
    $lt: ISODate("2013-10-01 00:00:00")
    }
}

})

It works very well on the console mongo, but i want to write it with map
reduce on the model to execute query.

How can i do it?
How can i write functions map, reduce and finalize to do execute this
query res =
Minterview.where(cond).map_reduce(map,reduce).out(:inline=>1).finalize(finalize).raw?

Thank you very much

The equivalent query in mysql is this:

SELECT canal, count(*) as contact, sum(count_sent) as
sent,sum(count_answers) as answer,sum(is_finished) as repondant
FROM minterviews
WHERE campaign_id = 90 and created_at >= "2013-01-01 " and created_at
<=“2013-10-01”
group by canal

I do somethink like this:

cond = {campaign_id: 90}
map = “function(){ emit(
{‘c’ : this.canal},
{‘contact’ : 1, ‘sent’ : this.count_sent, ‘answer’ :
this.count_answers, ‘repondant’ : this.is_finished}
);}”

    reduce = "function(key,values)
    {
          var ret = {'contact:'  0, 'sent' : 0, 'answer' : 0,

‘repondant’ : 0};
for(var i=0; i< values.length; i++)
{
ret.contact += values[i].contact;
ret.sent += values[i].sent;
ret.answer += values[i].answer;
ret.repondant += values[i].repondant;
}
return ret;
}"

res =
Minterview.where(cond).map_reduce(map,reduce).out(:inline=>1).raw

I have some error:

NoMethodError Exception: undefined method `split’ for nil:NilClass

Where is the problem?