# Find the last entry by user\_id

**URL:** <https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838>\
**Category:** Rails\
**Created:** [May 16, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838 "2008-05-16T20:43:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 16, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838/1 "2008-05-16T20:43:18Z")

</div>

How do I find the all records that are the latest from each user, by  
user\_id? I’m trying to get an :average working but I can’t solve it.  
This is my find:

Dada.average(:column\_with\_nr, :conditions =\> [“created\_at \> ?”,  
@date.to\_s(:db)], :order =\> ‘created\_at’, :select =\> ‘DISTINCT user\_id’)

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 16, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838/2 "2008-05-16T20:54:23Z")

</div>

Pål Bergström wrote:

> How do I find the all records that are the latest from each user, by  
> user\_id? I’m trying to get an :average working but I can’t solve it.  
> This is my find:
> 
> Dada.average(:column\_with\_nr, :conditions =\> [“created\_at \> ?”,  
> @date.to\_s(:db)], :order =\> ‘created\_at’, :select =\> ‘DISTINCT user\_id’)

:order =\> ‘created\_at desc’, :limit =\> 1, :group =\> ‘user\_id’

?

What also helps is to read your log, find your select statement, stick  
that into  
an editor, edit it, stick it into MySQL, and see what it does. Repeat  
until it  
works, then either retrofit it into your .average() statement, or just  
call  
count\_by\_sql on the raw SELECT.

As a style thing, there’s no harm in calling a raw SELECT statement from  
Rails,  
so long as you don’t go basing your whole architecture on it!

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 16, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838/3 "2008-05-16T20:57:42Z")

</div>

Phlip wrote:

> What also helps is to read your log, find your select statement, stick  
> that into  
> an editor, edit it, stick it into MySQL, and see what it does. Repeat  
> until it  
> works, then either retrofit it into your .average() statement, or just  
> call  
> count\_by\_sql on the raw SELECT.
> 
> As a style thing, there’s no harm in calling a raw SELECT statement from  
> Rails,  
> so long as you don’t go basing your whole architecture on it!

That’s true 🙂 Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 16, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/find-the-last-entry-by-user-id/137838/4 "2008-05-16T21:08:29Z")

</div>

> :order =\> ‘created\_at desc’, :limit =\> 1, :group =\> ‘user\_id’

Doesn’t work. I must do something wrong, or this is not suited for  
:average. I get a result all right, but it’s a hash or array, not an  
average value.
