我有一个rails应用程序,其中用户发布故事,其他用户可以喜欢它们,排名第一和最高5个故事的故事排在最前面的5个故事,现在这发生在整体基础上,我希望这发生每天都会自动,就像星期一一样,热门故事只会显示星期二星期一排名第一的故事会发生变化。
top_stories.html.erb
<% posts.each_with_index do |post, index| %>
<div class="post-panel">
<div class="top-story-rank">
<div class="count-button-wrapper">
<span class="count-button"><%= index + 1 %></span>
</div>
</div>
<%= render partial: 'posts/post_metadata', locals: { post: post } %>
<% if post.picture? %>
<div class="post-picture-wrapper">
<%= image_tag post.picture.url(:thumb) %>
</div>
<% end %>
<div class="main-body">
<h3 class="post-title"><%= link_to post.title, post %></h3>
<% if post.lead %>
<div class="post-body"><%= truncate(post.lead, length: 190, separator: ' ', escape: false) %></div>
<%= link_to "Read more", post, class: 'read-more' %>
<% end %>
</div>
<div class="post-stats clearfix">
<div id="post_<%= post.id %>_likes" class="pull-left">
<%= render partial: 'posts/likes', locals: { post: post } %>
</div>
<div id="post_<%= post.id %>_bookmarks" class="pull-right">
<%= render partial: 'posts/bookmarks', locals: { post: post } %>
</div>
<div class="response-count pull-right">
<% if post.responses_count > 0 %>
<%= link_to (pluralize(post.responses_count, "response")), post_path(post, anchor: 'responses'), class: 'response-count' %>
<% end %>
</div>
</div>
</div>
<% end %>
model.rb
scope :top_stories, ->(number) { order(likes_count: :desc).limit(number) }
我尝试了Whenever Gem,但这并不是我想要的。
答案 0 :(得分:0)
根据我的理解,您希望根据他们在特定日期获得的喜欢在页面上显示明智的故事。
下面提到的范围将根据特定日期的喜欢来获取故事:
scope :top_stories, -> (number) {where("to_char(created_at,'day') = ?", Date.today.strftime("%A").downcase).order(likes_count: :desc).limit(number)}
注意:我还没有测试过上面的代码,所以请检查语法,这只是根据特定日期显示记录的示例。