如何在模型级别强制主动记录 (Ruby) 预先加载?
我们想强制我们的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您需要为此调整您的default_scope。
It looks like you'll want to tweak your default_scope for this.