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
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
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