Currently, I am ordering records with counter cache that keeps track of the amount of likes they have, such as:
Post.order("COALESCE(likes_count, 0) DESC").limit(10)
Is there a way to incorporate a time limit on the likes of the cache that are used to sort the items with? Such as, include only likes created in the last 24 hours.
Likes are handled in this manner:
like.rb
belongs_to :liker, class_name: "User"
belongs_to :liked, class_name: "Post", :counter_cache => :likes_count
validates :liker_id, presence: true
validates :liked_id, presence: true
user.rb
has_many :likes, foreign_key: "liker_id", dependent: :destroy
has_many :liked_posts, through: :likes, source: :liked
post.rb
has_many :likes, foreign_key: "liked_id", dependent: :destroy
has_many :liker_users, through: :likes, source: :liker
Thanks a ton for any answers. Hugely appreciated.