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

**URL:** <https://www.ruby-forum.com/t/find-all-on-parent-where-child-nil/113035>\
**Category:** Rails\
**Created:** [August 26, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/find-all-on-parent-where-child-nil/113035 "2007-08-26T17:08:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [August 26, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/find-all-on-parent-where-child-nil/113035/1 "2007-08-26T17:08:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [August 27, 2007, 10:28am UTC](https://www.ruby-forum.com/t/find-all-on-parent-where-child-nil/113035/2 "2007-08-27T10:28:46Z")

</div>

with\_scope

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001024)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

set it to u’r needs 🙂

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [August 28, 2007, 12:11am UTC](https://www.ruby-forum.com/t/find-all-on-parent-where-child-nil/113035/3 "2007-08-28T00:11:38Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
Ryan W.  
Lead Developer  
Capocus!

> **[Atlanta video production](https://abra.capoc.us/)**
>
> Your Video Team Capocus! Studios is located in Fayetteville, GA. We produce weekly YouTube Video Series Content, create and launch Digital Courses, produce Podcasts, and develop Evergreen Webinars. YouTube Series ...

Productive Web Publishing
