[英]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 end
研訊 model
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
這是 schema.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
這給出了錯誤 Can't join 'Inquest' to association named 'votes'; 也許你拼錯了?
首先將其添加到您的調查表https://github.com/ryanto/acts_as_votable#caching
在調查 model 中添加以下協會。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.