Created
March 23, 2014 12:06
-
-
Save javiercr/9722206 to your computer and use it in GitHub Desktop.
Slow ActiveRecord query with custom SQL and will_paginate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@messages = Message.includes(:from_user, :to_user, :parent) | |
.where( | |
"messages.id IN ( | |
SELECT MAX(id) FROM messages GROUP BY COALESCE(parent_id, id) ORDER BY id DESC | |
)" | |
).order("messages.id DESC").scoped | |
@messages = @messages.paginate(page: params[:page]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment