please help solve the problem. tables: users: id: integer name: varchar
posts: id: integer title: varchar user_id: integer views: integer
models: User: class User < ActiveRecord::Base has_many :posts, dependent: :destroy end
Posts: class Post < ActiveRecord::Base belongs_to :user end
controller: def popular_diary @diaries = User.joins(:posts).group(:user_id).order('SUM(posts.views)') end
the result @diaries contains a collection users, sorted by the number of views. in ascending order (ASC). but I need to get a collection of users, sorted in descending order (DESC).
I have tried to do so: @diaries = User.joins(:posts).group(:user_id).order('SUM(posts.views) :DESC')
but I got an error message: 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