0

In punching_bag github page, it says I can run rake punching_bag:combine for combining hits.

when I run this rake I get error below:

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

What is wrong and how can I fix this issue?

Rubioli
  • 685
  • 6
  • 34

1 Answers1

0

Reason is, The ORDER BY clause can only be applied after the DISTINCT has been applied. Since only the fields in the SELECT statement are taken into consideration for the DISTINCT operations, those are the only fields may be used in the ORDER BY (from here).

So in punching_bag.rake, change:

punchable_types = Punch.uniq.pluck(:punchable_type)

TO

punchable_types = Punch.unscope(:order).uniq.pluck(:punchable_type)

and same with:

Punch.uniq.where(punchable_type: punchable_type).pluck(:punchable_id)

change to:

Punch.unscope(:order).uniq.where(punchable_type: punchable_type).pluck(:punchable_id)

Basically add unscope(:order) to begining if each request.

Community
  • 1
  • 1
Rubioli
  • 685
  • 6
  • 34