2016-12-12 55 views
0

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

原因是,ORDER BY子句只能在DISTINCT應用後應用。由於只有SELECT語句中的字段被考慮用於DISTINCT操作,因此這些唯一的字段可以在ORDER BY(from here)中使用。

所以在punching_bag.rake,變化:

punchable_types = Punch.uniq.pluck(:punchable_type)

TO

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)如果每個請求begining。