【发布时间】:2014-06-08 17:33:51
【问题描述】:
我想查询前 10 条记录。
所以,我从 Rails 控制台输入:
Log.all.limit(10).where({"username"=>"peeyush"}).explain
这给出了:
Log Load (0.8ms) SELECT "logs".* FROM "logs" WHERE "logs"."username" = 'peeyush' LIMIT 10
显然,LIMIT 10 会在以后发生。
我尝试跑步:
Log.all.first(10).where({"username"=>"peeyush"}).explain
但这会报错:
NoMethodError: undefined method `where' for #<Array:0x0000000539acd8>
应该如何执行查询?
【问题讨论】:
-
在
where条件之后你应该给first(10) -
那也不行!它提供了一个包含前 10 个日志的数组,其中用户名为 peeyush。
-
就查询而言,就是输出结果的权利。
-
Log.all.where({"username"=>"peeyush"}).first(10).explain -
这给出了错误
NoMethodError: undefined method 'explain' for #<Array:0x00000005ba0518>,因为返回值是大小为 10 的数组。此外,它并没有解决我的目的。
标签: sql ruby-on-rails activerecord ruby-on-rails-4