2
@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")

Kaminari 不使用组子句。如何优化此查询中的组子句?

4

2 回答 2

3

有同样的问题,像你一样修复它 - 通过在子句中提供适当的id值。group但是,显然它不需要替换您原来的 group 子句 - 只需附加一个额外的调用:

@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')
    .group('users.id')
于 2013-09-06T11:18:16.280 回答
2

我用这个修复了它:

@users = User.registred
        .includes(:items)
        .joins('LEFT OUTER JOIN items ON users.id=items.user_id')
        .group("users.id")
        .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')

@users = @users.page(params[:page])
于 2012-09-10T05:44:29.777 回答