Find last entry from each category

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

No ideas to solve this problem? :frowning:

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