使用 act as votable 获得用户最喜欢的帖子 gem rails

getting most liked posts by users using act as votable gem rails

我想显示过去 2 小时内帖子获得最多赞的前 10 个用户,我该怎么做 查询索引以获取用户

<%@users.each do |user| %>
  <%if user.id != current_user %>
  <%=user.email%>
  <%end%>
<%end%>

inquest.controller 指数

def index
@users =  User

.joins(inquests: :votes) .where('inquests.updated_at > ?', (Time.current - 2.hours)) .order('inquests.cached_weighted_average desc') .limit(10) @inquests = Inquest.all.order(created_at: :desc) @inquest = Inquest.new 结束

审讯模式

class Inquest < ApplicationRecord
acts_as_votable
belongs_to :user
has_many :comments, -> {order(:created_at => :desc)}, dependent: :destroy
has_many_attached :images 
validates :description, presence: true

validates :title, :presence => true, :length => { 
    :maximum => 250,
    :tokenizer => lambda { |str| str.scan(/\w+/) },
    :too_long  => "Please limit your summary to %{count} words"
  }
end

这里是scheema.rb文件

  create_table "inquests", force: :cascade do |t|
t.string "title", null: false
t.text "description"
t.integer "creator_id"
t.integer "cached_votes_total", default: 0
t.integer "cached_votes_score", default: 0
t.integer "cached_votes_up", default: 0
t.integer "cached_votes_down", default: 0
t.integer "cached_weighted_score", default: 0
t.integer "cached_weighted_total", default: 0
t.float "cached_weighted_average", default: 0.0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "cached_scoped_subscribe_votes_total", default: 0
t.integer "cached_scoped_subscribe_votes_score", default: 0
t.integer "cached_scoped_subscribe_votes_up", default: 0
t.integer "cached_scoped_subscribe_votes_down", default: 0
t.integer "cached_weighted_subscribe_score", default: 0
t.integer "cached_weighted_subscribe_total", default: 0
t.float "cached_weighted_subscribe_average", default: 0.0
t.index ["creator_id"], name: "index_inquests_on_creator_id"
end
  create_table "votes", force: :cascade do |t|
t.string "votable_type"
t.integer "votable_id"
t.string "voter_type"
t.integer "voter_id"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["votable_id", "votable_type", "vote_scope"], name: 
"index_votes_on_votable_id_and_votable_type_and_vote_scope"
t.index ["votable_type", "votable_id"], name: 
 "index_votes_on_votable"
t.index ["voter_id", "voter_type", "vote_scope"], name: 
"index_votes_on_voter_id_and_voter_type_and_vote_scope"
t.index ["voter_type", "voter_id"], name: 
"index_votes_on_voter"
 end

这是错误无法加入 'Inquest' 到名为 'votes' 的协会;也许你拼错了?

首先将此添加到您的调查中 table https://github.com/ryanto/acts_as_votable#caching

在调查模型中添加以下关联。

has_many :votes, as: :votable 和 运行 下面的查询

User
   .joins(inquests: :votes)
   .where('inquests.updated_at > ?', (Time.current - 2.hours))
   .order('inquests.cached_weighted_average desc')
   .limit(10)

User
  .joins('INNER JOIN inquests ON inquests.user_id = users.id INNER JOIN votes ON votes.votable_id = inquests.id')
  .where('inquests.updated_at > ?', (Time.current - 2.hours))
  .order('inquests.cached_weighted_average desc')
  .limit(10)