acts_as_votable按upvotes排序 [英] acts_as_votable ordering by upvotes

查看:192
本文介绍了acts_as_votable按upvotes排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 <$订购问题的数量,我一直无法找到任何有效的排序问题c $ c> acts_as_votable gem

以下是我的upvote和index方法:

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

def索引
@comment = Comment.find params [:comment_id]
@questions = @ comment.questions
end

和我的问题观点:

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

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

我应该在视图和控制器中放置什么?

解决方案

这个特殊的gem有一个缓存迁移,您可以运行。
$ b https://github.com/ryanto/acts_as_votable#caching

  class AddCachedVotesToPosts< ActiveRecord :: Migration 
def self.up
add_column:posts,:cached_votes_total,:integer,:default => 0
add_column:posts,:cached_votes_score,:integer,:default => 0
add_column:posts,:cached_votes_up,:integer,:default => 0
add_column:posts,:cached_votes_down,:integer,:default => 0
add_index:posts,:cached_votes_total
add_index:posts,:cached_votes_score
add_index:posts,:cached_votes_up
add_index:posts,:cached_votes_down

#取消注释此行以强制缓存现有投票
#Post.find_each(&;:update_cached_votes)
结束

def self.down
remove_column:posts,:cached_votes_total
remove_column:posts,:cached_votes_score
remove_column:posts,:cached_votes_up
remove_column:posts,:cached_votes_down
end
end



我的建议是使用示例代码创建新的迁移,并使用它进行排序。



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

http://guides.rubyonrails.org/active_record_querying.html#ordering



例如:

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

这会根据票数来排序。


I haven't been able to find anything that works so far for ordering questions by number of upvotes using the acts_as_votable gem.

Here are my upvote and index methods:

 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

and my questions view:

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

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

What should I put in the view and the controller to make this work?

解决方案

This particular gem has a caching migration you can run as well.

https://github.com/ryanto/acts_as_votable#caching

class AddCachedVotesToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :cached_votes_total, :integer, :default => 0
    add_column :posts, :cached_votes_score, :integer, :default => 0
    add_column :posts, :cached_votes_up, :integer, :default => 0
    add_column :posts, :cached_votes_down, :integer, :default => 0
    add_index  :posts, :cached_votes_total
    add_index  :posts, :cached_votes_score
    add_index  :posts, :cached_votes_up
    add_index  :posts, :cached_votes_down

    # Uncomment this line to force caching of existing votes
    # Post.find_each(&:update_cached_votes)
  end

  def self.down
    remove_column :posts, :cached_votes_total
    remove_column :posts, :cached_votes_score
    remove_column :posts, :cached_votes_up
    remove_column :posts, :cached_votes_down
  end
end

My suggestion would be to create a new migration with the sample code and use that to sort against.

Once you've created that migration, you can sort on one of those columns:

http://guides.rubyonrails.org/active_record_querying.html#ordering

For example:

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

That will sort by the number of up votes.

这篇关于acts_as_votable按upvotes排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆