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:
http://www.rubybrain.com/api/ruby-1.8.6/doc/index.html

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs