如何显示帖子有超过 5 票并在 1 小时内发布?

How to show posts have more than 5 votes and be posted in 1hour?

这是我写的,它与仅显示 post 已 post 在 1 小时内完成,

def feed
  Micropost.where("created_at >= ?", Time.zone.now - 10.minutes)
end

我的微型post型号:

class Micropost < ActiveRecord::Base

    has_many :votes, as: :voteable
    ...

  def total_votes
    self.up_votes - self.down_votes
  end

  def up_votes
    self.votes.where(vote: true).size
  end

  def down_votes
    self.votes.where(vote: false).size
  end
end

我的投票模型

class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :voteable, polymorphic: true
  validates_uniqueness_of :user, scope: [:voteable_id, :voteable_type]
end

相关Link:

我不喜欢这个解决方案,因为它需要对您的数据库进行多次点击,但对于少量(数百甚至数千)项,它应该可以正常工作。

def feed
  posts = Micropost.where("votes>=5 and created_at >= ?", 1.hour.ago)
  return posts.select { |post| post.total_votes > 5 }
end