gpt4 book ai didi

Ruby Datamapper 计数不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:58 26 4
gpt4 key购买 nike

当我这样做时来自 irb:

Router.all(:email=>"blake@gmail.com")

我得到了与该电子邮件关联的所有路由器的列表。但是当我这样做时:

Router.count(:email=>"blake@gmail.com")

我总是得到0

我也看过这个问题:Ruby Datamapper .count always returns 0但我仍然不知道为什么它不起作用。

-- 更新 #1 --

这是 Router.all 命令的输出。如您所见,我得到了结果。

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

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

最佳答案

正如 piyush 指出的那样,Router.all(:email=>"blake@gmail.com").count是正确的方法。在您调用其中一种“触发”方法之前,DataMapper 不会运行实际查询,例如 all , first , last .这允许在调用最终 .all 之前链接多个方法。运行复合查询。

如果是Router.count(:email=>"blake@gmail.com") ,您正在对“空”DataMapper 对象运行计数,该对象已被初始化但其查询尚未运行。

关于Ruby Datamapper 计数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14393004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com