尽早停止 meta_search 执行 sql

发布于 2024-10-28 23:02:02 字数 778 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2024-11-04 23:02:02

如果您在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文