请帮助解决问题。 表:
users:
id: integer
name: varchar
posts:
id: integer
title: varchar
user_id: integer
views: integer
模型:
User:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
Posts:
class Post < ActiveRecord::Base
belongs_to :user
end
控制器:
def popular_diary
@diaries = User.joins(:posts).group(:user_id).order('SUM(posts.views)')
end
结果@diaries包含一个集合用户,按视图数量排序。按升序排列(ASC)。但我需要获得一组用户,按降序排序(DESC)。
我试过这样做:
@diaries = User.joins(:posts).group(:user_id).order('SUM(posts.views) :DESC')
但是我收到了一条错误消息:
SQLite3::SQLException: near ":DESC": syntax error: SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" GROUP BY user_id ORDER BY SUM(posts.views) :DESC
答案 0 :(得分:2)
@pavan是对的。必须是“DESC&#39;不是&#39;:DESC&#39;