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.
As it seems there's no functionality in counter cache itself to accomplish this, I ended up doing a little detour, but which in itself is actually is quite simple. It simply narrows down the objects which to rank with counter cache to those that got new likes in 24 hours.
application_helper.rb
view.html.eb
If anyone has any improvement ideas on this, it's greatly appreciated, I'm sure, by a lot of people. I'm not going to mark this an accepted answer in case someone comes up with the ultimate way to do this.