sql - 想要在 Rails 中使用 Count、Group by 和 Order DESC 进行订购

标签 sql ruby-on-rails activerecord

这是我的疑问,我有下表:

  create_table "followings", force: :cascade do |t|
    t.integer "follower_id"
    t.integer "followed_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["followed_id"], name: "index_followings_on_followed_id"
    t.index ["follower_id", "followed_id"], name: "index_followings_on_follower_id_and_followed_id", unique: true
    t.index ["follower_id"], name: "index_followings_on_follower_id"
  end

这是我的用户表:

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "photo"
    t.string "coverimage"
    t.string "fullname"
    t.string "username"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

我需要创建最受关注的前 10 个用户,因此在本例中,是最重复的 follow_id,但我无法根据 ActiveRecord 中刚刚创建的列进行排序。我正在尝试这样的事情:

@userst = Following.where(:group  => "followed_id", :select => "device_id, COUNT(folloing_id)", sort: :dsc)

我可能做错了什么?

这是我的用户 Controller (如您所见,顶部尚未完成):

class UsersController < ApplicationController
  before_action :authenticate_user!

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @posts = @user.posts.ordered_by_most_recent
  end

  def edit
    @user = User.find(params[:id])
  end

  def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end

  def top
    @userst = User.where()
  end
end

只是为了让您知道这是用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :authentication_keys => [:username]

  validates :fullname, presence: true, length: { maximum: 20 }
  validates :username, presence: true, length: { maximum: 20 }

  validates_uniqueness_of :username

  has_many :posts
  has_many :active_followings, class_name:  "Following",
  foreign_key: "follower_id",
  dependent:   :destroy
  has_many :passive_followings, class_name:  "Following",
  foreign_key: "followed_id",
  dependent:   :destroy
  has_many :following, through: :active_followings, source: :followed
  has_many :followers, through: :passive_followings, source: :follower

  mount_uploader :photo, FileUploader
  mount_uploader :coverimage, FileUploader

    # Follows a user.
  def follow(other_user)
    following << other_user
  end

  # Unfollows a user.
  def unfollow(other_user)
    following.delete(other_user)
  end

  # Returns true if the current user is following the other user.
  def following?(other_user)
    following.include?(other_user)
  end
end

最佳答案

假设您有一个用户模型,并且它包含与以下模型的 has_many 关系,您可以尝试:

User.joins(:followings)
    .order('COUNT(followings.followed_id) DESC')
    .group('users.id')
    .limit(10)

关于sql - 想要在 Rails 中使用 Count、Group by 和 Order DESC 进行订购,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60519399/

相关文章:

ruby-on-rails - Django 中的 Ruby on Rails 模板 "extend"

ruby-on-rails - 如何处理 Rails 应用程序中重用的对象

sql - 在 C# 中按总和 Linq to SQL 分组

ruby-on-rails - Rails 对现有的 ActiveRecord 结果数组进行分页

MySQL - 无法在数据透视表中正确分组

ruby-on-rails - rails 4 : Notifications by user

ruby-on-rails - 在 Rails 中将列数据类型从 bool 值更改为整数

ruby-on-rails - 如何永久忽略 ActiveRecord::Base 类中的数据库列?

sql - 如何更正BAT文件中的Select Spool

c# - Linq 仅查询每个唯一 ID 的前 N ​​行