2016-12-28 66 views
1

我想让我的数据库中的第一个42岁的人。Mongoid查询的等于运算符

我怎样才能让这个查询的工作:

Person.where(:Age.eq => 42).first 

非但没有查询结果的,我得到:

undefined method `eq' for :Age:Symbol 
+0

Person.where(年龄:42)。首先 – Aby

+0

如果不存在则尝试这种因此它会创建并返回:Person.where(年龄:42).first_or_create – Aby

回答

0

尝试: -

Person.where(:age => 42).first 

它将查询Personage = 42

OR

查询这样: -

Person.find_by age: 42 

的find_by方法找到的第一个与此条件匹配的记录

See the documentation

+0

It works Person.where(:age => 42).first thank's :) – user3146542

0

什么刚:

Person.where(Age: 42).first 

+0

第一个命题作品完美,感谢的 – user3146542