如何将`:include` 添加到default_scope? [英] How to add `:include` to default_scope?
本文介绍了如何将`:include` 添加到default_scope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
搜索'net,我发现我应该使用:include
,但这似乎并没有改变生成的SQL查询:
Searching the 'net, I found that I should use :include
, but that does not seem to change the SQL query that is generated:
def Post #model
default_scope :order => 'created_at DESC', :include => :author
end
有或没有 :include
,SQL 是相同的(即它只是从帖子表中选择).
With or without the :include
, the SQL is the same (i.e. it is only selecting from the posts table).
有什么方法可以做到这一点?
What is the way to do this?
推荐答案
如果你这样做了会怎样
default_scope { includes(:author).order('created_at ASC') }
这是在 Rails API 中为 记录的方式default_scope
&scope
,而不是您正在使用的哈希参数方法.
This is the way that it’s documented in the Rails API for default_scope
& scope
, rather than the hash parameter method you're using.
这篇关于如何将`:include` 添加到default_scope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文