Ruby on rails 按向上投票的方式进行可投票排序

Ruby on rails 按向上投票的方式进行可投票排序,ruby-on-rails,rubygems,Ruby On Rails,Rubygems,到目前为止,我还没有找到任何能够通过使用 以下是我的投票和索引方法: def upvote @question = Question.find params[:id] @question.liked_by current_user redirect_to comment_questions_path end def index @comment = Comment.find params[:comment_id] @questions = @comment.questio

到目前为止,我还没有找到任何能够通过使用

以下是我的投票和索引方法:

 def upvote
  @question = Question.find params[:id]
  @question.liked_by current_user
  redirect_to comment_questions_path
 end

 def index
 @comment = Comment.find params[:comment_id]
 @questions = @comment.questions
 end
我的问题是:

<%= div_for(question) do %>
<% if question.votes.size > 0 %>
<div class="verifiedanswer">
<%= question.body %>
</div>

<% else %>
<div class="answercontainer2">
<%= question.body %>
</div>
<% end %>

0 %>

我应该在视图和控制器中放置什么才能使其工作?

这个特定的gem有一个缓存迁移,您也可以运行

类AddCachedVotesToPosts0 添加列:posts,:cached\u voces\u score,:integer,:default=>0 添加列:posts、:cached\u vows\u up、:integer、:default=>0 添加列:posts、:cached\u vows\u down、:integer、:default=>0 添加索引:帖子、缓存的投票总数 添加索引:帖子、缓存的投票 添加索引:帖子、缓存的投票 添加索引:posts,:cached\u投票\u down #取消对此行的注释以强制缓存现有投票 #Post.find_each(&:update_cached_投票) 结束 def自动关闭 删除\u列:帖子,:缓存的\u投票\u总数 删除\u列:帖子,:缓存的\u投票\u分数 删除列:帖子、缓存的投票 删除列:发布,:缓存的\u投票\u关闭 结束 结束 我的建议是使用示例代码创建一个新的迁移,并使用它进行排序

创建迁移后,可以对其中一列进行排序:

例如:

<% Post.order(:cached_votes_up).each do |post| %>
  ... html goodness here ...
<% end %>

... 我的天啊。。。

这将根据投票数进行排序。

对不起,rails是新手,我不确定如何在我看来实现这一点。添加了更多。你知道如何在计票或按日期加权时只包括最近的选票吗?试图实现一个排序方式:“hot”,我个人更喜欢
Post.order(:cached\u voces\u score)。反转
,这将考虑到任何下行投票。
<% Post.order(:cached_votes_up).each do |post| %>
  ... html goodness here ...
<% end %>