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 -- Posted via http://www.ruby-forum.com/.
I''m not 100% sure, but you might try this: Project.find(:all, :include ''Line'', :conditions => ["Line.closed_at is NULL"]) HTH On 5/2/06, mich <nospam@thanks.com> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060502/e5aced4f/attachment.html
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. On 5/2/06, Larry White <ljw1001@gmail.com> wrote:> > I''m not 100% sure, but you might try this: > > Project.find(:all, :include ''Line'', :conditions => ["Line.closed_at is > NULL"]) > > HTH > > > On 5/2/06, mich <nospam@thanks.com> 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060502/3e8ef9da/attachment-0001.html
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-- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.