简体   繁体   English

如何从rails中的活动记录对象获取FOUND_ROW()?

[英]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.

相关问题 如何在 Rails 上查询日文活动记录? - How can I query in Japanese on Rails active record? 如何从活动记录对象获取数据库sql值? - How to get database sql values from an active record object? 如何在Rails的CSV导出文件中从活动记录模型向行添加记录数组 - How to add an array of records to row from active-record model in a CSV export file in rails 如何从Rails中的活动记录结果数组中删除记录? - How to delete an record from array of active record result in rails? 如何从表 B 中查询关系活动记录,表 A 有两个字段连接表 B 的主键? - How can I query relational active record from table B with table A has two fields connect the table B's primary key? 如何获取一个SQL文件并将其导入Rails Active Record基础连接? - How can I take a SQL file and have it import into Rails Active Record base connection? 如何从codeigniter活动记录获取result_array的列表到row_array - how to get list of result_array into a row_array from codeigniter active record Rails Active Record .paginate,Get First page(row),如果指定的页面(行)在数据库表中不存在 - Rails Active Record .paginate, Get First page(row), if page (row) specified doesn't exist in database table Rails Active Record,从has_many:through关系中获取相关记录,并且where子句通过 - Rails Active Record, get related record from has_many :through relationship with where clause on through record 如何获取rails活动记录属性对应的SQL值 - How to get rails active record attributes corresponding SQL values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM