Ruby on rails 如何更深入地加载两次

Ruby on rails 如何更深入地加载两次,ruby-on-rails,eager-loading,Ruby On Rails,Eager Loading,在我的帖子模型中,我尝试用每个评论的作者加载用户(帖子的所有者)、收藏夹和评论: Post.all.includes(:user, :original => {:favorite, :comments => :author }) 但它不起作用,我得到了错误 syntax error, unexpected ',', expecting => ...(:user, :original => [{:favorite, :comments => :author}...

在我的帖子模型中,我尝试用每个评论的作者加载用户(帖子的所有者)、收藏夹和评论:

 Post.all.includes(:user, :original => {:favorite, :comments => :author })
但它不起作用,我得到了错误

syntax error, unexpected ',', expecting => ...(:user, :original => [{:favorite, :comments => :author}... ...

有人能告诉我问题出在哪里吗?

正如错误所示,这只是一个语法错误。您使用的
{:favorite,:comments=>:author}
不是有效的哈希

使用数组作为
:original
的值来执行所需操作:

Post.includes(:user, :original => [:favorite, :comments => :author]).all
您的困惑可能源于忽略了一些并非所有情况下都需要的大括号。用显式添加的大括号重新编写上述内容可能会更清楚:

Post.includes(:user, {:original => [:favorite, {:comments => :author}]}).all