On Mon, Feb 21, 2011 at 2:48 PM, Paul Bergstrom <li...@ruby-forum.com>wrote:

> I think I'm on the right track. But I get the first post for each user,
> not the latest. This is what I use.
>
> Model.all(:select => "*, max(id)", :group => :user_id)
>
> How do get the latest?
>
> In Rails 3 syntax,
Post.where(:user_id => given_user_id).group(:user_id).order(:updated_at
DESC).limit(number).group_by(:&user_id)
First, group the results by user_id.
Then, order the updated_at or created_at in DESC order.
Then, you can limit the results for each user by passing a number.
Then, finally return a hash of all the results with keys as user_id for
easier and better retrieval.

> --
> 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@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to