在punching_bag
github页面中,它说我可以运行rake punching_bag:combine
来组合点击。
当我运行此rake
时,我收到以下错误:
SELECT DISTINCT "punches"."punchable_type" FROM "punches" ORDER BY punches.average_time DESC
01 PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
01 LINE 1: ...unches"."punchable_type" FROM "punches" ORDER BY punches.av...
01 ^
01 : SELECT DISTINCT "punches"."punchable_type" FROM "punches" ORDER BY punches.average_time DESC
01 rake aborted!
01 ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
01 LINE 1: ...unches"."punchable_type" FROM "punches" ORDER BY punches.av...
01
有什么问题,如何解决这个问题?
答案 0 :(得分:0)
原因是,ORDER BY子句只能在应用DISTINCT后应用。由于DISTINCT操作只考虑SELECT语句中的字段,因此可以在ORDER BY(from here)中使用这些字段。
所以在punching_bag.rake
中,更改:
punchable_types = Punch.uniq.pluck(:punchable_type)
以强>
punchable_types = Punch.unscope(:order).uniq.pluck(:punchable_type)
与
相同 Punch.uniq.where(punchable_type: punchable_type).pluck(:punchable_id)
更改为:
Punch.unscope(:order).uniq.where(punchable_type: punchable_type).pluck(:punchable_id)
基本上添加unscope(:order)
以开始每个请求。