# Listing parents from child conditon

**URL:** <https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241>\
**Category:** Rails\
**Created:** [May 2, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241 "2006-05-02T13:31:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 2, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/1 "2006-05-02T13:31:36Z")

</div>

Hi,

How can I grab all parents in a table, depending on a condition in the  
children table ?

Fx.  
def list  
@projects = Project.find(:all)  
end

will find all projects in my table. Now let’s say I want show a similar  
list, for all open projects… I define an open project, as all lines  
(Line has belongs\_to :Project) that do not yet have a closed\_at  
timestamp set.

So basically, I want all projects, where project.line.closed\_at is NULL.

How would I do that ?

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 2, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/2 "2006-05-02T14:31:24Z")

</div>

Read the documentation for find(). Look at the syntax for the  
:conditions parameter.

mich wrote:

> Hi,
> 
> How can I grab all parents in a table, depending on a condition in the  
> children table ?
> 
> Fx.  
> def list  
> @projects = Project.find(:all)  
> end
> 
> will find all projects in my table. Now let’s say I want show a similar  
> list, for all open projects… I define an open project, as all lines  
> (Line has belongs\_to :Project) that do not yet have a closed\_at  
> timestamp set.
> 
> So basically, I want all projects, where project.line.closed\_at is NULL.
> 
> How would I do that ?
> 
> /mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 2, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/3 "2006-05-02T15:25:42Z")

</div>

Steve K. wrote:

> Oh, sorry… I didn’t read carefully enough! Sorry I came off trollish.  
> 😉

No worries.

> Try something like
> 
> @projects = Project.find(:all, :conditions =\> “lines.closed\_at IS NULL”,  
> :include =\> :lines)

Cheers.

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 2, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/4 "2006-05-02T15:16:19Z")

</div>

Oh, sorry… I didn’t read carefully enough! Sorry I came off trollish.  
😉

Try something like

@projects = Project.find(:all, :conditions =\> “lines.closed\_at IS NULL”,  
:include =\> :lines)

mich wrote:

> Hi,
> 
> How can I grab all parents in a table, depending on a condition in the  
> children table ?
> 
> Fx.  
> def list  
> @projects = Project.find(:all)  
> end
> 
> will find all projects in my table. Now let’s say I want show a similar  
> list, for all open projects… I define an open project, as all lines  
> (Line has belongs\_to :Project) that do not yet have a closed\_at  
> timestamp set.
> 
> So basically, I want all projects, where project.line.closed\_at is NULL.
> 
> How would I do that ?
> 
> /mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/5 "2006-05-03T18:59:29Z")

</div>

I’m not 100% sure, but you might try this:

Project.find(:all, :include ‘Line’, :conditions =\> [“Line.closed\_at is  
NULL”])

HTH

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/listing-parents-from-child-conditon/59241/6 "2006-05-03T18:59:29Z")

</div>

If that doesn’t work you can use find by sql

Project.find\_by\_sql(“select DISTINCT project.\* from project, line  
where  
line.closed is null and project.id = line.project\_id”)

There may be a better ‘rails-y’ way, but i’m not sure

The agile programming with rails book has a very extensive discussion of  
find. It’s very worthwhile to have.
