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 <[email protected]>

> 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 <[email protected]>
>
> 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 [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<rubyonrails-talk%[email protected]>
>> .
>> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to