@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没有使用group子句。如何优化此查询中的group子句?
答案 0 :(得分:3)
有同样的问题,通过在id
子句中提供适当的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')
答案 1 :(得分: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])