我有一个用户和字体的应用程序。我设置的ARRSystem和USers都可以喜欢,喜欢可以在字体显示页面上看到。但是,当我尝试使用Like对它们进行排序时
def index
@fonts = Font.find_with_reputation(:likes, :all, {:order => 'likes desc'})
end它不起作用。不会发生排序。我希望在我的导航菜单中有“最受欢迎”和“最新”,但不能让它工作。我使用的是这个文件:https://github.com/NARKOZ/activerecord-reputation-system/tree/rails4
发布于 2013-12-01 05:25:26
我找到了一个解决办法,我有一些图片,我正在尝试根据投票结果对图片进行分类:
我的解决方案是:
在images_controller.rb中(索引方法)
image_ids = ActiveRecord::Base.connection.execute("SELECT target_id FROM rs_reputations WHERE target_type = 'Image' ORDER BY value DESC")
image_ids = image_ids.map { |item| item = item[0] }
@images = []
image_ids.each { |id| @images << Image.find(id) }我看了看我的服务器日志,它看起来不是用一个查询来拉取所有图像,而是对每个项目进行“find_with_reputation”查询(因此无法排序)。它也不会查询'value‘。
ReputationSystem::Reputation Load (0.2ms) SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 14 AND "rs_reputations"."target_type" = 'Image' LIMIT 1
ReputationSystem::Reputation Load (0.3ms) SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 15 AND "rs_reputations"."target_type" = 'Image' LIMIT 1
ReputationSystem::Reputation Load (0.3ms) SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 17 AND "rs_reputations"."target_type" = 'Image' LIMIT 1解决方案:要么将查询压缩为一个查询,要么将每个查询结果放入一个数组中,然后按值排序。
编辑:这在MySQL开发中工作得很好,但在PostgreSQL中就不那么好用了。我收到一个PG::RESULT对象,我不太知道如何处理它。
https://stackoverflow.com/questions/19411686
复制相似问题