Named_scope

Hi,

I’m trying to make a named_scope that only lists Products with at
least 1 comment. product.comments.size > 0

*Product

has_many :comments

*Comment

belongs_to :product

I haven’t figured out how to using count/size for my conditions.

Regards

product.comments.length is what you’re looking for.

Also check out my 2 favorite versions of the ruby and rails API’s
here:

Ruby:

Rails:
http://www.railsbrain.com/api/rails-2.0.2/doc/


Jon Kinney
Ruby on Rails Architect
Inacom Information Systems

On Tue, Jul 22, 2008 at 12:12 PM, casp [email protected] wrote:

I’m trying to make a named_scope that only lists Products with at
least 1 comment. product.comments.size > 0

I think it would be enough to force an inner join:

named_scope :commented, :joins => :comments

=> SELECT * FROM “products” INNER JOIN “comments” ON

comments.product_id = products.id