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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Jul 22, 5:12 am, casp <ggus...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, Jul 22, 2008 at 12:12 PM, casp <ggustav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to make a named_scope that only lists Products with at > least 1 comment. product.comments.size > 0I 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---