Pål Bergström | 16 May 20:40

Find the last entry by user_id


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')
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Phlip | 16 May 20:53
Picon

Re: Find the last entry by user_id


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!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Pål Bergström | 16 May 20:57

Re: Find the last entry by user_id


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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Pål Bergström | 16 May 21:08

Re: Find the last entry by user_id


> :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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Gmane