防止为每个评论调用upvote模型

时间:2015-06-16 13:01:44

标签: ruby-on-rails ruby database performance model

我有三个模型:User,Comment和Upvote。 User-to-Comment具有一对多关系,Comment-to-Upvote具有一对多关系,User-to-Upvote具有一对多关系。

我想做类似于Stackoverflow上的upvoting的事情。因此,当您进行upvote / downvote时,即使您刷新页面或在几天/几周后返回页面,箭头也会突出显示并保持突出显示。

目前我这样做:

<% if Upvote.voted?(@user.id, comment.id) %>
  <%= link_to '^', ... style: 'color: orange;'%>
<% else %>
  <%= link_to '^', ... style: 'color:black;'%>
<% end %>

voted?方法如下所示:

  def self.voted?(user_id, comment_id)
    find_by(comment_id: comment_id, user_id: user_id).present?
  end

因此,如果我在一个页面上有10个评论,这将从我的数据库中加载10次upvote,只是为了检查它是否存在!

必须有更好的方法来做这件事,但我认为我的大脑停止了工作,所以我无法想到任何事情。

4 个答案:

答案 0 :(得分:7)

假设您已正确设置关系

# user.rb
class User
  has_many :upvotes
end

我们可以加载评论,当前用户和他的赞成票:

# comments_controller.rb
def index
  @comments = Comment.limit(10)
  @user = current_user
  user_upvotes_for_comments = current_user.upvotes.where(comment_id: @comments.map(&:id))
  @upvoted_comments_ids = user_upvotes_for_comments.pluck(:comment_id)
end

然后在视图中更改if条件:

# index.html.erb
<% if @upvoted_comments_ids.include?(comment.id) %>
  <%= link_to '^', ... style: 'color: orange;'%>
<% else %>
  <%= link_to '^', ... style: 'color:black;'%>
<% end %>

只需要2个DB查询。希望它有所帮助。

答案 1 :(得分:4)

如果您希望通过单个查询处理它,我们可以通过以下方式执行此操作。

让我们确保关系正常

# user.rb
class User < ActiveRecord::Base
  has_many :comments
  has_many :upvotes
end

# comment.rb
class Comment < ActiveRecord::Base
  belongs_to :user
  has_many :upvotes
end

# upvote.rb
class Upvote < ActiveRecord::Base
  belongs_to :user
  belongs_to :comment
end

然后在控制器中

def index
  current_user = User.first # current_user may come from devise or any authentication logic you have.
  @comments = Comment.select('comments.*, upvotes.id as upvote').joins("LEFT OUTER JOIN upvotes ON comments.id = upvotes.comment_id AND upvotes.user_id = #{current_user.id}")
end

在视野中

# index.html.erb
<% @comment.each do |comment| %>
  <% link_color =  comment.upvote ? 'orange' : 'black' %>
  <%= link_to '^', ...style: "color: #{link_color}" %>
<% end %>
# And all of your logics ;)

答案 2 :(得分:2)

如果您每页仅限N条评论,那么您可以使用limitoffset方法在两个查询中执行此操作,以返回第1个,第2个,。 .. ithN页面的ith条评论,例如(语法可能不合适,Ruby不是我的主要语言)

comment_ids = 
    Comments.select("comment_id")
            .where(user_id: user_id)
            .order(post_date/comment_id/whatever)
            .offset(per_page * (page_number - 1)) // assumes 1-based page index
            .limit(per_page)

这会为您提供comment_ids的列表,您可以使用它来查询Upvote:

upvoted_comments = 
    Upvotes.select("comment_id")
           .where(user_id: user_id, comment_id: comment_ids)

如果您按comment_ids中也存在的列对Upvote进行排序(例如,如果您按comment_id排序),则可以替换{ {1}}使用范围查询设置查询。

Upvote放入哈希值表示你好了 - 如果upvoted_comments在哈希值中,那么它就被提升了,否则就没有了。

答案 3 :(得分:0)

我不确定这会避免在这种状态下出现多余的查询,但也许您可以在获取评论时包含upvotes:

@comments = Comment.includes(:upvotes).where(foo: 'bar').limit(10)

然后在视图中:

<%=
  link_color =  comment.upvotes.map(&:user_id).include?(@user.id) ? 'orange' : 'red'
  link_to '^', ...style: "color: #{link_color}"
%>