views:

640

answers:

2

Pretend I have a model, Post which has_many :comments. How can I only display posts that have comments?

I am somewhat comfortable with named_scope but I don't know how I can put Post.comments (or self.comments) in the :conditions hash which expects symbols.

class Post < ActiveRecord::Base
     has_many :comments
     named_scope :with_comments, :conditions => [#self.comments.length > 0]
end

What do I write in the commented area?

Thanks!

+2  A: 

You should be able to just join against your comments table, making sure to select the distinct rows

named_scope :with_comments, :joins => :comments, :select => 'DISTINCT posts.*'
slillibri
looks awesome. How can I pass in arguments allowing me to manually enter in attribute names. my posts table has a pk called postid and the comments table has an fk called post_id. I know how to manually set pks and fks in the associations, how do I do it with named_scope? thanks again, this really is a quality response.
Just replace the :joins option with the join you want. You probably want something like the following, :joins => 'INNER JOIN "comments" ON comments.post_id = posts.postid'
slillibri
+2  A: 

Better might be to put a counter_cache on Post.

class Comment < AR:Base
  belongs_to :post, :counter_cache => true
end

Then you only need to do 1 query instead of two.

Post.find(:all, :conditions => ["counter_cache > 0"])

BJ Clark
+1: Much more efficient than doing a join.
musicfreak