Hi, I''m having trouble creating the right code to query the following: - I have a table of "trails" and a table of "trailreports" - "trails" has a "hasmany" relationship with "trailreports" - "trailreports" has a field "trail_id", "report" and "created_at" field. I would like to query the list of "trails" that have the most resent "trailreports" based on the "created_at" field which is a timestamp. How do I got about this ? Thank You, Nick,
Something along the lines of... Trail.find(:all, :include => [:trailreports], :order => [''trailreports.created_at DESC'']) On Mon, Aug 3, 2009 at 12:57 PM, Nick<nhinze-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I''m having trouble creating the right code to query the following: > > - I have a table of "trails" and a table of "trailreports" > - "trails" has a "hasmany" relationship with "trailreports" > - "trailreports" has a field "trail_id", "report" and "created_at" > field. > > I would like to query the list of "trails" that have the most resent > "trailreports" based on the "created_at" field which is a timestamp. > > How do I got about this ? > > Thank You, > > Nick, > > >-- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC design // development // hosting w/Ruby on Rails planetargon.com robbyonrails.com twitter.com/planetargon aim: planetargon +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4068 [fax]
Nick wrote:> Hi, > > I''m having trouble creating the right code to query the following: > > - I have a table of "trails" and a table of "trailreports" > - "trails" has a "hasmany" relationship with "trailreports" > - "trailreports" has a field "trail_id", "report" and "created_at" > field. > > I would like to query the list of "trails" that have the most resent > "trailreports" based on the "created_at" field which is a timestamp. > > How do I got about this ?Perhaps something like Trail.find(:all, :joins => :trailreports, :order => ''trailreports.created_at desc'', :limit => 10) ?> > Thank You, > > Nick,Best, -- Marnen Laibow-Koser marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.