I want to be able to display messages and sort them according to several criteria, first by the number of votes that they have, and secondly, by the date they were created. I don’t want messages that are more than a week to be displayed so that only messages from the last week. I tried to do this:
<%= render @posts.sort_by { |post| post.votes.count if post.created_at < 1.week.ago.utc }.reverse %>
but it gave me an error comparing NilClass with 2 unsuccessful
I know that the code works by simply sorting the messages by the number of votes, but I also want to limit the time for someone to tell me how to do this. I'm still a beginner, so I'm sorry for the simplicity.
source
share