ruby on rails - How to eager load with a condition -


i have classic post has_many relationship on comments. want fetch of approved posts related comments have been approved. have approved scope in place each not sure how enable on comments.

this have far returns comments approved postings. best way without having specify condition on has_many limit use of association other queries.

post.approved.includes(:comments)

your example used in documentation. should use association:

class post < activerecord::base   has_many :approved_comments, :class_name => 'comment', :conditions => ['approved = ?', true] end  post.includes(:approved_comments) 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -