Rails 急切加载

发布于 2024-09-02 16:22:55 字数 329 浏览 2 评论 0原文

你好, 我有一个测试模型,其中有很多问题,还有一个问题,其中有很多答案...... 当我使用 :include => 查询测试时[:问题,{:问题=> :answers}] ActiveRecord 又进行了两个查询来获取问题,然后获取答案 - 它不会加入它们!当我使用 :joins ActiveRecord 进行查询时,ActiveRecord 会进行查询,但稍后当我需要 Test.questions 或 Test.questions.answers 时,ActiveRecord 会再次进行这两个额外的查询!后来,当我枚举日志中的问题或答案时,我看到每个对象的其他查询,但它有缓存标签......

这正常吗?

HI,
I have a Test model, which has_many questions, and Question, which has_many answers...
When I make a query for a Test with :include => [:questions, {:questions => :answers}] ActiveRecord makes two more queries to fetch the questions and then to fetch the answers - it doesn`t join them!!! When I do the query with :joins ActiveRecord makes the query, but later when I need the Test.questions or Test.questions.answers ActiveRecord makes again those 2 extra queries!!! And later when I enumerate the questions or answers in the log I see other queries for each object, but it has Cache tag...

Is this normal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我的痛♀有谁懂 2024-09-09 16:22:55

我同意 shteef 的观点,当您执行以下操作时会发生什么:

:include => { :questions => :answers }

I agree with shteef, what happens when you do the following:

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