尽早停止 meta_search 执行 sql
在 docs 中说:
MyObject.search()
返回MetaSearch::Builder 的实例(类似于 ActiveRecord::Relation)。但就我而言,当我这样做时,我会得到一个对象集合,因为 sql 查询已发送到数据库。
我想要这样的事情:
search = MyObject.search() # no sql-query should be done here
count = search.count # count sql done
objects = search.all # select sql done - maybee with pagination
有人知道如何阻止 Meta_search 过早进行查询吗?
->好吧,我的 shell 中发生了一些神秘的事情:
search = MyObject.search() # queries the database
search = MyObject.search(); 0 # stores a MetaSearch-Object in search
控制台似乎在每个命令之后调用一个额外的方法
in the docs it's said:
MyObject.search()
returns an instance of MetaSearch::Builder (something like ActiveRecord::Relation). But in my case when I do this I get a collection of objects because the sql-query is send to the database.
I would like to have something like this:
search = MyObject.search() # no sql-query should be done here
count = search.count # count sql done
objects = search.all # select sql done - maybee with pagination
does anyone know how to stop Meta_search from doing the the query to early?
-> ok, something mysterious going on in my shell:
search = MyObject.search() # queries the database
search = MyObject.search(); 0 # stores a MetaSearch-Object in search
the console seems to call an extra method after each comand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 irb 中进行测试,请注意返回的对象会被检查。对于元搜索构建器,这意味着关系会被检查。如果您查看一下relation.rb 中的ActiveRecord 的inspect 方法,您将看到它调用to_a,该方法执行查询并返回结果。
If you're testing in irb, be aware that returned objects are inspected. In the case of a MetaSearch builder, this means the relation gets inspected. If you have a look at ActiveRecord's inspect method, in relation.rb, you'll see that it calls to_a, which executes the query and returns the results.