> 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 som
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]
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
> How would I write a named_scope that checks if the parent has at least
> one associated child?
>
> For example,
> class Post < ActiveRecord::Base
>
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,
4 matches
Mail list logo