act_as_votable由upvotes排序

到目前为止,我还没有找到任何可以使用acts_as_votable gem通过upvotes订购问题的方法。

这是我的upvote和索引方法:

  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 

我的问题观点:

   0 %> 

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

这个特殊的gem还有一个可以运行的缓存迁移。

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 

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

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

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

例如:

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

这将按投票数量排序。