蒙古人发现者无法正常工作? [英] Mongoid finders not working?

查看:103
本文介绍了蒙古人发现者无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了rails3 + mongoid应用程序,当我打开rails控制台时,发现者似乎都不起作用- http ://d.pr/FNzC

I have set up a rails3+mongoid application and when I open the rails console, none of the finders seem to work - http://d.pr/FNzC

User.all
User.find(:all, :conditions => { first_name => "John" })

均返回:

#<Mongoid::Criteria
  selector: {},
  options:  {}>

我做错什么了吗?

推荐答案

好的,所以这是使蒙古族人对新人感到恼火的部分原因.人们期望像User.all之类的方法实际上在实际上返回Criteria对象时返回一个数组.

Okay, so this is part of what makes mongoid irritating for newcomers. People expect methods like User.all to actually return an array when it really just returns the Criteria object.

为了提供可链接方法和其他精美查询机制的语法糖,Mongoid似乎使用了惰性加载类型的东西.

In order to provide the syntatic sugar of chainable methods and other fancy query mechanisms, Mongoid seems to use a lazy loading type thing.

您可以这样做:

#array index
User.all[0]

#first/last
User.all.first

#each over things, print out all the users
User.all.each {|u| p u}

#edit, I forgot to include this, which is probably what you really want
#this spits out an array
User.all.to_a

很难从ActiveRecord那里快速地验证事情是否对新用户有效,其中User.all仅返回一个数组.

It makes it difficult to quickly verify that things are working for newcomers from ActiveRecord where User.all just returns an array.

这篇关于蒙古人发现者无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆