我找到了一个解决方法,我有一些图像,我试图根据 upvotes 对 desc 进行排序:
我的解决方案:
在 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”查询每个项目(因此无法排序)。它也不会查询“价值”。
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
解决方案:要么将 Query 压缩为一个,要么将每个查询结果压入一个数组,然后按值排序。
编辑:这在使用 MySQL 的开发中效果很好,但在 PostgreSQL 中却不是那么好。我收到一个我不太知道如何处理的 PG::RESULT 对象。