Hi, can anybody help me with to specify a SQL-statement which should do following: Example table (published_at is a DateTime field) id | content | published_at | author_id -------------------------------------------- 1 | foo | 2010 | 1 2 | foo | 2009 | 1 3 | foo | 2008 | 1 4 | foo | 2010 | 2 5 | foo | 2009 | 2 6 | foo | 2008 | 2 7 | foo | 2007 | 3 Now, how can I get the last entry (max published_at) for each author The result should be a array with news items 1,4,7 Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No ideas to solve this problem? :( -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 May 2010 10:09, Sebastian Kranz <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> how can I get the last entry (max published_at) for each author > The result should be a array with news items 1,4,7Have you looked at using the "group_by" method of Enumberable? http://apidock.com/rails/Enumerable/group_by You could use it to group by author_id and then select the latest entry from each array. In raw SQL: SELECT id, author_id, max(published_at) FROM ''entries group by user_id Or in Rails finder: Entry.all(:select => "*, max(published_at)", :group => :author_id) Have you read the ActiveRecord API docs? It''s all in there.... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.