thumbs_up
thumbs_up copied to clipboard
plusminus_tally broken in Postgres with recent Arel
A recent version of Arel (I haven't isolated which one) has broken the complex #plusminus_tally query:
TestThumbsUp#test_plusminus_tally_count:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: ...vote WHEN 't' THEN 1 WHEN 'f' THEN -1 ELSE 0 END) AS plusmin...
^
: SELECT COUNT(items.*, SUM(CASE votes.vote WHEN 't' THEN 1 WHEN 'f' THEN -1 ELSE 0 END) AS plusminus_tally, COUNT(votes.id) AS vote_count) AS count_items_all_sum_case_votes_vote_when_t_then_1_when_f_then_1, items.id, items.user_id, items.name, items.description AS items_id_items_user_id_items_name_items_description FROM "items" LEFT OUTER JOIN votes ON items.id = votes.voteable_id AND votes.voteable_type = 'Item' GROUP BY items.id, items.user_id, items.name, items.description
The workaround currently is to call #to_a on the method before counting:
Item.tally.except(:order).to_a.count
Essentially Arel is nesting COUNTs, which doesn't make sense. I'm not sure how to fix this one. Anyone with some Arel expertise, I'd love some help with this.