*在底部更新*当寻找不存在的用户时,我得到:Mongoid::Errors::DocumentNotFoundinUsersController#showProblem:Document(s)notfoundforclassUserwithid(s)22.Summary:WhencallingUser.findwithanidorarrayofids,eachparametermustmatchadocumentinthedatabaseorthiserrorwillberaised.Thesearchwasfortheid(s):22...(1total)andthefollowin
我有一个这样定义的模型:classFooinclude::Mongoid::Documentfield:name,type:Stringfield:followed_bars,type:Arrayfield:favorite_bars,type:Arrayend我像这样创建了一个Foo对象:foo=Foo.new(name:"Test")foo.save在我的数据库中,当我键入db.foo.find()时,我可以看到我刚刚创建的对象。然后,在我的应用程序中,我尝试这样做:foo=Foo.firstfoo.push(:followed_bars,"hello")每次我都会收到错误消息:A
如何将任何类的对象限制为一个。我的课看起来像:classSpeakerincludeMongoid::Documentfield:name,:type=>Stringend我只想让speaker的单个实例。一种方法是添加一个验证,该验证将检查Speaker类中已经存在的对象数量。有ruby做事的方式吗? 最佳答案 如何使用Singleton模块? 关于ruby-on-rails-阻止创建一个类的多个对象,我们在StackOverflow上找到一个类似的问题:
在一个简单的mongoid数据模型上,用户有很多评论,我想在用户至少写1条评论时奖励他一个特定的徽章。所以我设置了一个这样的观察者:classCommentBadgeObserver1badge=CommentBadge.create(:title=>"Commentbadge")user.award(badge)endendenduser.award方法:defaward(badge)self.badges下面的测试失败了(但我猜这是正常的,因为观察者是在后台执行的?)it'shouldawardtheuserwithcommentbadge'do@comment=Factory(:
我一直在摆弄Mongo,但无法让这个简单的示例发挥作用。我只是想检索集合中的所有文档:require'mongoid'#configuration...classCategoryincludeMongoid::Documentfield:name,type:StringendCategory.eachdo|test|putstest.inspectend我收到错误:未定义方法'each'forCategory:Class(NoMethodError)。与数据库的连接建立良好,名为categories的集合包含一些文档。 最佳答案 C
给定一个任意的mongoid文档,我如何将其转换为JSON并包含任何嵌入式结构,而不是在我的to_json语句中特别包含这些结构。例如:#!/usr/bin/envrubyrequire'mongoid'require'json'require'pp'classDocincludeMongoid::DocumentincludeMongoid::Timestampsfield:doc_specific_info,type:Stringembeds_many:personsendclassPersonincludeMongoid::Documentfield:role,type:Stri
我需要将嵌入式文档转换成它自己的集合,以便它可以从另一个集合中引用。假设我有一个Parent嵌入了许多Child。我在想一些事情:Parent.all.eachdo|p|p.childs.all.eachdo|c|c.raw_attributes['parent_id']=p.idendp.save!#willsaveparentandcascadepersistallchildsontotheirowncollend这是一个选项吗?理想情况下,我会在控制台中运行它,我只会将mongoid映射从embed_*更改为has_*,因此我不需要更改其余代码或使用另一个集合作为暂存。
我已经通过MapReduce获取了一些ID。我已经按照一些标准对这些ID进行了排序,现在我需要按照这个特定的顺序获取这些对象:MyModel.find(ids)对吧?但它返回的对象不在存储id的顺序中。看起来这和MyModel.where(:_id.in=>ids)它不会以与存储ID相同的顺序返回获取的对象。现在我可以做到了ids.map{|id|MyModel.find(id)}这将完成这项工作,但它会多次敲击数据库。 最佳答案 正在研究类似的问题并找到了更简洁的解决方案:objs=MyModel.find(ids).sort_b
我的ruby模型,像这样:classUserincludeMongoid::Documentfield:first_name,type:Stringfield:birthdate,type:Datevalidates:first_name,:birthdate,:presence=>trueend像这样输出一个对象:{_id:{$oid:"522884c6c4b4ae5c76000001"},birthdate:null,first_name:null,}我的主干项目不知道如何处理_id.$oid。我找到这篇文章和代码:https://github.com/rails-api/acti
我有以下继承自Entry模型的mongoid模型:classEntry::TwitterInteger,:default=>0field:retweeted,:type=>Boolean,:default=>falsefield:favorited,:type=>Boolean,:default=>false#in_reply_to_screen_name,in_reply_to_status_id_str,in_reply_to_user_id_strfield:reply,:type=>Hashfield:from,:type=>Hash#user:id_str,name,scre