Find(:all) on parent where child.nil?

How can I find(:all) on a parent model only for objects where a has_one
child is nil?


class Parent < ActiveRecord::Base
has_one :child
class Child < ActiveRecord::Base
belongs_to :parent

So, I want to do:

object = Parent.find(:all) where the only rows returned match

I want this to happen on the database level because we will be dealing
thousands of rows. Is there an ActiveRecord way to handle this, or am I
going to have to break out to find_by_sql for this?



set it to u’r needs :slight_smile:

Thanks Shai. I’ve been dipping into with_scope the last couple of days
it’s really interesting… especially when it comes to nesting. I’m not
quite sure how I can utilize it in my situation though where I’m doing a
left join on two models with a has_one relationship. Every example of
with_scope I’ve seen as been acting within a single model. I’ll keep
with it. In the meantime, find_by_sql is getting the job done.

On 8/27/07, Shai R. [email protected] wrote:

Ryan W.
Lead Developer
Productive Web Publishing

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs