2011-12-24 67 views
0

“self”代表Contact模式。 此查询将查找联系人的所有已完成任务。 任务模型具有字段/键:数组类型的assigned_contacts。 所以查询是在数组内搜索。MongoMapper到Mongoid:如何在数组内搜索?

def assigned_tasks_completed 
    self.company.tasks.all(:assigned_contacts => self.id.to_s, :completed => true) 
end 

如何在Mongoid中做到这一点?

另一个问题: @ contact.set(:a - > a,:b - > b,:c - > c)的Mongoid中的等价物是update_attributes!

但是在Mongoid中也有一套方法。 set & update_attributes!有什么区别!在Mongoid?

回答

0

我假设你的任务是一个单独的文档。所以,你可以只是where取代all,它会工作

def assigned_tasks_completed 
    self.company.tasks.where(:assigned_contacts => self.id.to_s, :completed => true) 
end 

为了您的另外一个问题,

两个mongoid set & update attributes是内部使用mongodb $set。但区别在于mongoid设置只接受单个字段更新,update_attributes接受多个。