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?

Models

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

So, I want to do:

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

I want this to happen on the database level because we will be dealing
with
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?

Ryan

with_scope

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001024

set it to u’r needs :slight_smile:

Thanks Shai. I’ve been dipping into with_scope the last couple of days
and
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
playing
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
Capocus!
http://abra.capoc.us
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