繁体   English   中英

act_as_votable由upvotes排序

[英]acts_as_votable ordering by 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

我的问题观点:

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

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

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

这个特殊的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 %>

这将按投票数量排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM