如何在模型级别强制主动记录 (Ruby) 预先加载?

发布于 2024-10-10 13:22:16 字数 328 浏览 0 评论 0原文

我们想强制我们的 Post 模型立即加载所有评论。

现在我们必须在 find(:all) 处指定急切加载,如下所示:

Post.all(:include => [ :comment ])

有没有一种方法可以在 Post 模型级别强制急切加载默认值,而不是必须在每个查找中执行此操作?像下面这样:

class Post < ActiveRecord::Base
  has_many :comments, :include <all comments>  # eager load??

We would like to force our Post model to eager load all comments.

Right now we have to specify the eager loading at the find(:all), like below:

Post.all(:include => [ :comment ])

Is there a way to force an eager loading default at the Post model level, rather than having to do it in every find? Something like below:

class Post < ActiveRecord::Base
  has_many :comments, :include <all comments>  # eager load??

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

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

发布评论

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

评论(1

心意如水 2024-10-17 13:22:16

看来您需要为此调整您的default_scope

It looks like you'll want to tweak your default_scope for this.

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