@users = User.registred
.includes(:items)
.joins('LEFT OUTER JOIN items ON users.id=items.user_id')
.group(User.column_names.collect do |c| "#{User.table_name}.#{c}" end.join(','))
.order('CASE WHEN LENGTH(users.profile_picture) > 0 THEN 0 ELSE 1 END, followers_count DESC, bibbed_items_count DESC, SUM(items.user_likes_count) DESC, users.id')
どこregistered = where("deactivated_at IS NULL AND last_sign_in_at IS NOT NULL")
カミナリはグループ句で動作しません。このクエリでグループ句を最適化する方法は?