How would I write a named_scope that checks if the parent has at least one associated child? For example, class Post < ActiveRecord::Base has many :comments named_scope :commented_on, :conditions => "comments.count > 0" class Comment < ActiveRecord::Base belongs_to :post Thanks, jeff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i think use named_scope is not elegant way. use class method? no idea.you can give me your sample proj.i can test this way. 2009/12/2 Jeff Blasius <jeff.blasius-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> How would I write a named_scope that checks if the parent has at least > one associated child? > > For example, > class Post < ActiveRecord::Base > has many :comments > > named_scope :commented_on, :conditions => "comments.count > 0" > > > class Comment < ActiveRecord::Base > belongs_to :post > > > > Thanks, > jeff > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- tommy xiao E-mail: xiaods(AT)gmail.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i got it. reference this url: http://refactormycode.com/codes/209-rails-has_many-count sample code: class User has_many :messages do def by_type(type, options={}) options[:count_only] ||= false messages = find(:all, :conditions => [ ''type = ?'', type ]) options[:count_only] ? messages.size : messages end end end OPTION 1: without helper in view <h1>Messages:</h1> <ul> <li>Industry Expert: <%= current_user.messages.by_type(''Industry Expert'', :count_only => true) -%></li> <li>Media: <%= current_user.messages.by_type(''Media'', :count_only => true) -%></li> <li>PR: <%= current_user.messages.by_type(''PR'', :count_only => true) -%></li> </ul> 2009/12/2 tommy xiao <xiaods-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> i think use named_scope is not elegant way. use class method? no idea.you > can give me your sample proj.i can test this way. > > 2009/12/2 Jeff Blasius <jeff.blasius-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > How would I write a named_scope that checks if the parent has at least >> one associated child? >> >> For example, >> class Post < ActiveRecord::Base >> has many :comments >> >> named_scope :commented_on, :conditions => "comments.count > 0" >> >> >> class Comment < ActiveRecord::Base >> belongs_to :post >> >> >> >> Thanks, >> jeff >> >> -- >> >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >> > > > -- > tommy xiao > E-mail: xiaods(AT)gmail.com >-- tommy xiao E-mail: xiaods(AT)gmail.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> How would I write a named_scope that checks if the parent has at least > one associated child?First, its better to describe the problem a liitle bit, what I undestand if you want to find only the post that have post, Isnt it ? Look this example : I wrote by heart, so probably you will have some errors describe Post, "find methods" do context "find recent comments" do before :each do (1..10).each {|n| Comment.create(valid_comment_attributes) } post = Post.create! valid_post_attributes end it "should not return post with no comments" do Post.commented_posts.should_not include post end end end So you need to include the comments, but the normal :include => :comments make by default I think make LEFT OUTER join, and what you need is a INNER JOIN, so you can specify that in the :join parameter. named_scope :commented_posts, lambda { {:joins => ''INNER JOIN comments ON comments.post_id = posts.id''}} its that enough , maybe, but you will discover after a while a problem some problems like, that you have repeated_posts, and then you should and remove the duplication in another named_scope, or in a the same one it "should not have repeated post" do post = Comment.create(valid_comment_attributes :post=>Post.first) Post.commented_posts.should have_exactly(10).posts end> For example, > class Post < ActiveRecord::Base > has many :comments > > named_scope :commented_on, :conditions => "comments.count > 0" > > > class Comment < ActiveRecord::Base > belongs_to :post > > > > Thanks, > jeff > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > > >-- ------------------------------------- Pedro Del Gallego Email : pedro.delgallego-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.