# Rails Database Query

**URL:** <https://www.ruby-forum.com/t/rails-database-query/173140>\
**Category:** Rails\
**Created:** [August 3, 2009, 10:37pm UTC](https://www.ruby-forum.com/t/rails-database-query/173140 "2009-08-03T22:37:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [August 3, 2009, 10:37pm UTC](https://www.ruby-forum.com/t/rails-database-query/173140/1 "2009-08-03T22:37:51Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [August 4, 2009, 1:06am UTC](https://www.ruby-forum.com/t/rails-database-query/173140/2 "2009-08-04T01:06:23Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [August 4, 2009, 1:17am UTC](https://www.ruby-forum.com/t/rails-database-query/173140/3 "2009-08-04T01:17:04Z")

</div>

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[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://robbyonrails.com/](http://robbyonrails.com/)  
[http://twitter.com/planetargon](http://twitter.com/planetargon)  
aim: planetargon

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