Ruby Datamapper计数不起作用 [英] Ruby Datamapper count not working
问题描述
在执行操作时来自irb:
From irb when I do:
Router.all(:email=>"blake@gmail.com")
我获得了与该电子邮件关联的所有路由器的列表.但是当我这样做时:
I get a list of all the routers associated with that email. But when I do:
Router.count(:email=>"blake@gmail.com")
我总是得到0
我也研究了这个问题: Ruby Datamapper .count始终返回0 ,但我仍然不知道为什么它不起作用.
I've also looked at this question: Ruby Datamapper .count always returns 0 but I still don't know why it isn't working.
-更新#1-
这是Router.all命令的输出.如您所见,我得到了结果.
Here is the output of the Router.all command. As you can see I get results back.
1.9.3-p362 :003 > Router.all(:email=>"blake@gmail.com")
=> [#<Router @id=8 @email="blake@gmail.com" @hostname="router0">, #<Router @id=9 @email="blake@gmail.com" @hostname="router0">, #<Router @id=10 @email="blake@gmail.com" @hostname="router0">, #<Router @id=11 @email="blake@gmail.com" @hostname="router0">, #<Router @id=13 @email="blake@gmail.com" @hostname="router0">, #<Router @id=14 @email="blake@gmail.com" @hostname="router0">, #<Router @id=15 @email="blake@gmail.com" @hostname="router0">, #<Router @id=16 @email="blake@gmail.com" @hostname="router0">]
但是当我按照建议执行Router.count时,它仍然返回0
But when I do Router.count as suggested it still is returning 0
1.9.3-p362 :004 > Router.count(:conditions => ["email = ?", "blake@gmail.com"])
=> 0
1.9.3-p362 :005 > Router.count(:conditions => "email = 'blake@gmail.com'")
=> 0
推荐答案
您可以require 'dm-aggregates'
,之后您可以
Router.count(:email => "blake@gmail.com")
它将转化为
SELECT COUNT(*) FROM routers WHERE "email" = 'blake@gmail.com'
(尽管您会在Router.all(:email => "blake@gmail.com").count
和dm-aggregates
中得到相同的语句.)
(Although you'll get the same statement with Router.all(:email => "blake@gmail.com").count
with dm-aggregates
.)
这篇关于Ruby Datamapper计数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!