Forum: Ruby on Rails listing parents from child conditon

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adb3e4d29fba0d4360f225eba6ba47d7?d=identicon&s=25 mich (Guest)
on 2006-05-02 13:31
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
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-05-02 14:31
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
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-05-02 15:16
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
Adb3e4d29fba0d4360f225eba6ba47d7?d=identicon&s=25 mich (Guest)
on 2006-05-02 15:25
Steve Koppelman 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.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-05-03 18:59
(Received via mailing list)
I'm not 100% sure, but you might try this:

Project.find(:all, :include 'Line',  :conditions => ["Line.closed_at is
NULL"])

HTH
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-05-03 18:59
(Received via mailing list)
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.
This topic is locked and can not be replied to.