[英]How can I get FOUND_ROW()s from an active record object in rails?
When querying the database with: 使用以下方法查询数据库时:
@robots = Robot.all(:condition => [:a => 'b'], :limit => 50, :offset => 0)
What is the best way to get the total number of rows without the :limit
? 获取没有
:limit
的行总数的最佳方法是什么?
In raw MySQL you could do something like this: 在原始MySQL中,您可以执行以下操作:
SELECT SQL_CALC_FOUND_ROWS * FROM robots WHERE a=b LIMIT 0, 50
SELECT FOUND_ROWS();
Is there an active record way of doing this? 这样做是否有积极的记录方式?
This works for me: 这对我有用:
ps = Post.all(:limit => 10, :select => "SQL_CALC_FOUND_ROWS *")
Post.connection.execute("select found_rows()").fetch_hash
=> {"found_rows()"=>"2447"}
This will probably not work for joins or anything complex, but it works for the simple case. 这可能不适用于连接或任何复杂的操作,但它适用于简单的情况。
Robot.count actually is the solution you want. Robot.count实际上是你想要的解决方案。
Reading one of the comments above, it looks like you may have a misunderstanding of how .count works. 阅读上面的一条评论,看起来你可能对.count如何工作有误解。 It returns a count of all the rows in the table only if there's no parameters.
仅当没有参数时,它才返回表中所有行的计数。
but if you pass in the same conditions that you pass to all/find eg: 但如果你传递的条件相同,你传递给所有/找到例如:
Robot.count(:conditions => {:a => 'b'})
.count() will return the number of rows that match the given conditions. .count()将返回与给定条件匹配的行数。 Just to be obvious - you can even save the condition-hash as a variable to pass into both - to reduce duplication, so:
显而易见 - 您甚至可以将条件哈希保存为变量以传递到两者 - 以减少重复,因此:
conds = {:a => 'b'}
@robots = Robot.all(:conditions => conds, :limit => 50)
@num_robots = Robot.count(:conditions => conds)
That being said - you can't do an after-the-fact count on the result-set (like in your example). 话虽这么说 - 你不能对结果集进行事后计数(就像在你的例子中一样)。 ie you can't just run your query then ask it how many rows would have been found.
即你不能只运行你的查询,然后询问它将找到多少行。 You do actually have to call .count on purpose.
你实际上必须故意打电话给.count。
search = Robot.all(:condition => ["a=b"], :offset => 0)
@robots = search[0..49]
@count = search.count
That should get what you want, gets all the Robots for counting and then sets @robots
to the first 50. Might be a bit expensive on the resource front if the Robots table is huge. 这应该得到你想要的,得到所有的机器人计数,然后将
@robots
设置为前50。如果机器人表很大,可能在资源方面有点贵。
You can of course do: 你当然可以这样做:
@count=Robot.all(:condition => ["a=b"], :offset => 0).count
@robots=Robot.all(:condition => ["a=b"], :limit => 50, :offset => 0)
but that will hit the database twice on each request (although rails does have query caching). 但是这会在每个请求上击中数据库两次(尽管rails确实有查询缓存)。
Both solutions only use active record so are database independent. 两种解决方案仅使用活动记录,因此与数据库无关
What do you need the total returned by the query for? 您需要查询返回的总数是多少? if its pagination look into Will_paginate (Railscast) which can be extended with AJAX etc...
如果它的分页调查Will_paginate(Railscast)可以用AJAX等扩展...
尝试find_by_sql可能有帮助。
Is @robots.size
what you're looking for? 是
@robots.size
你在寻找什么? Or Robot.count
? 还是
Robot.count
?
Otherwise, please clarify. 否则,请澄清。
I think hakunin is right. 我认为hakunin是对的。
You can get no of row return by query by just chekcing the size of resulting array of query. 通过查询生成的查询数组的大小,您可以通过查询获得行返回。
@robots = Robot.find_by_sql("Your sql")
or
@robots = Robot.find(:all , :conditions=>["your condiitons"] )
@robots.size or @robots.count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.