I want to display the top 10 users whose posts got the most likes in past 2 hours, how can I do that inquest index to get user
<%@users.each do |user| %>
<%if user.id != current_user %>
<%=user.email%>
<%end%>
<%end%>
inquest.controller index
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
inquest 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
here is scheema.rb file
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
this is giving the error Can't join 'Inquest' to association named 'votes'; perhaps you misspelled it?
First add this to your inquests table https://github.com/ryanto/acts_as_votable#caching
Add below association in inquest model.
has_many :votes, as: :votable
and run below query
User
.joins(inquests: :votes)
.where('inquests.updated_at > ?', (Time.current - 2.hours))
.order('inquests.cached_weighted_average desc')
.limit(10)
OR
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)