【发布时间】:2020-03-04 05:06:10
【问题描述】:
这是我的疑惑,我有下表:
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)
我做错了什么?
这是我的用户控制器(如您所见,顶部尚未完成):
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
【问题讨论】:
-
您使用的是什么 Rails 版本?您尝试的查询返回什么?
folloing_id是错字吗? -
@SebastianPalma 嗨,我正在使用 Rails 6.0.2.1,我正在尝试获得最受关注用户的前 10 名。 Followed_id 和 Follower_id 是对 User_id 的索引引用
标签: sql ruby-on-rails activerecord