Rails Database Query

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,

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
http://www.marnen.org
[email protected]

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[email protected] wrote:

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 R.
Chief Evangelist, Partner

PLANET ARGON, LLC
design // development // hosting w/Ruby on Rails

http://robbyonrails.com/
http://twitter.com/planetargon
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]