ruby-on-rails – 由upvotes排序的acts_as_votable

到目前为止,我还没有找到任何可以通过使用 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,:cached_Votes_up,:cached_Votes_down,: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 %>

这将按投票数量排序.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...