加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:54:50 所属栏目:百科 来源:网络整理
导读:到目前为止,我还没有找到任何可以通过使用 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 inde
到目前为止,我还没有找到任何可以通过使用 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 %>

这将按投票数量排序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读