Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Art.all(:order => "created_at desc", :limit => 10, :group => "user_id")
- Art Load (18.4ms) SELECT "arts".* FROM "arts" GROUP BY user_id ORDER BY created_at desc LIMIT 10
- ActiveRecord::StatementInvalid: PGError: ERROR: column "arts.id" must appear in the GROUP BY clause or be used in an aggregate function
- LINE 1: SELECT "arts".* FROM "arts" GROUP BY user_id ORDER BY crea...
- a | b
- ---|---
- 1 | 1
- 1 | 2
- 2 | 3
- arts = Art.all(:order => "created_at desc", :limit => 10)
- grouped_arts = arts.group_by {|art| art.user_id}
- # now you have a hash with following structure in grouped_arts
- # {
- # user_id1 => [art1, art4],
- # user_id2 => [art3],
- # user_id3 => [art5],
- # ....
- # }
- SELECT arts.* FROM arts
- WHERE (arts.user_id, arts.created_at) IN
- (SELECT user_id, MAX(created_at) FROM arts
- GROUP BY user_id
- ORDER BY MAX(created_at) DESC
- LIMIT 10)
- ORDER BY created_at DESC
- LIMIT 10
- Art.where("(arts.user_id, arts.created_at) IN
- (SELECT user_id, MAX(created_at) FROM arts
- GROUP BY user_id
- ORDER BY MAX(created_at) DESC
- LIMIT 10)").
- order("created_at DESC").
- page(params[:page]).
- per(params[:per])
Add Comment
Please, Sign In to add comment