Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plusminus_tally broken in Postgres with recent Arel #102

Open
bouchard opened this issue Jun 20, 2015 · 0 comments
Open

plusminus_tally broken in Postgres with recent Arel #102

bouchard opened this issue Jun 20, 2015 · 0 comments

Comments

@bouchard
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant