Ruby on rails 急于加载activerecord\u信誉系统投票
使用,我看到我的博客索引页面正在为每个博客文章调用SQL查询,以查找投票数。如何加载投票以便合并这些查询 对于每个条目,我看到的SQL如下所示Ruby on rails 急于加载activerecord\u信誉系统投票,ruby-on-rails,activerecord,eager-loading,Ruby On Rails,Activerecord,Eager Loading,使用,我看到我的博客索引页面正在为每个博客文章调用SQL查询,以查找投票数。如何加载投票以便合并这些查询 对于每个条目,我看到的SQL如下所示 SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 8 AND "rs_reputations"."target_type" = 'Pos
SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 8 AND "rs_reputations"."target_type" = 'Post' LIMIT 1
在我的posts\u controller中,我目前渴望加载writer(writer有许多:posts)
我该如何添加:投票数?我已经尝试过了
@posts = Post.includes(:writer, :rs_reputation).paginate(page: params[:page]), :per_page => 10)
及
两者都不起作用
我的帖子模型如下
post.rb
belongs_to: :writer
has_reputation :votes, source: :writer, aggregated_by: sum
事实证明,在最新版本之前,这很难做到。在尝试此操作之前,请确保您的gem是最新的,但现在您可以使用以下工具轻松地进行即时加载:
evaluated_by(reputation_name, source [, scope])
例如:
Post.evaluated_by(:votes, @writer)
可以在github上找到讨论线程
更新:仔细检查,这种方法对你不起作用。它会加载特定作者所做的所有评价,而不是每个帖子的声誉。我会深入研究这个问题,但是你也可以考虑把你的问题发布到Github上。这个gem的开发者反应非常迅速,可能会在几天内为您提供一个解决方案
工作解决方案:我在github上问了一下,得到了答案。它应该是这样工作的。查询您的帖子,如:
Post.find_with_reputation(:reputation_name, :all)
然后得到如下投票值:
post.votes
Post.find_with_reputation(:reputation_name, :all)
post.votes