Is there any way to eager load a named_scope from an association?
I have my Article model:
class Article < ActiveRecord::Base has_many :comments end
and my Comment model:
class Comment < ActiveRecord::Base belongs_to :article named_scope :approved, :conditions => { :approved => true } named_scope :unapproved, :conditions => { :approved => false } end
I could eager load all comments for an article with:
@article = Article.find(params[:id], :include => :comments)
How can I do the same, but only for approved comments?
It is not built-in to rails at this time, but Ryan Daigle created a plugin called utility_scopes that adds a with() scope so you can do:Blog Post: http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know Github Repo: http://github.com/yfactorial/utility_scopes
[Updated per comment]
My bad. I didn’t read well enough. I don’t think there’s anything that will let you call on an association named_scope like that. The closest thing I can think of would be creating a named_scope on Article:
Hope this helps.