1

我写了一段代码急于负荷已加载的收集一些协会:预紧协会与现有的范围

@articles= Article.find_by_sql("SELECT * FROM articles WHERE blabla")  
ActiveRecord::Associations::Preloader.new(@articles, {:comments => {:user => :permissions}}).run 

我在我的文章类中定义准备一个范围,贪婪加载在一些文章协会几个级别:

class Article << ActiveRecord::Base 
    [...]  

    scope :eager_loading_for_comments, includes(:comments => {:user => :permissions}) 
end 

我可以在我的第一个代码中使用这个范围?一种方法那样:

ActiveRecord::Associations::Preloader.new(@articles, :eager_loading_for_comments).run 

或者:

ActiveRecord::Associations::Preloader.new(@articles, Article.eager_loading_for_comments).run 

谢谢!

回答

0

为什么不按照其他任何方式调用示波器,例如,

@articles = Article.eager_loading_for_comments 

+0

没了,我的问题(这里不外露,太复杂的很快,这是一个巨大的查询与许多加入不参与范围表。)力量我到一个find_by_sql,所以我不能直接用这个经典的方式使用范围。 – ronnieonrails

0

怎么样的地方链接到范围:

Article.eager_loading_for_comments.where("blah blah blah") 
+0

nope,我的问题(这里没有公开,太复杂了。很快,它是一个很大的查询,有很多连接到表中并没有涉及范围)强迫我去find_by_sql,所以我不能直接用这个经典的方法使用范围。 – ronnieonrails