Skip to content

ActiveRecord::QueryMethods#reverse_sql_order mangles functions #1697

Closed
titanous opened this Issue Jun 14, 2011 · 3 comments

4 participants

@titanous

For example Test.order('COALESCE(updated_at, created_at) DESC').last will generate the malformed SQL SELECT "tests".* FROM "tests" ORDER BY COALESCE(updated_at DESC, created_at) ASC LIMIT 1.

Full example case: https://gist.github.com/1025033

This is caused by the naive split on commas at https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb#L308

@franckverrot

The quickest workaround would be to use

Test.order('COALESCE(updated_at, created_at) DESC').last

instead of

Test.order('COALESCE(updated_at, created_at) ASC').first

We could parse the order clause but it would be a pretty large (slow?) regexp to handle all the cases.

@arunagw arunagw pushed a commit to arunagw/rails that referenced this issue Aug 14, 2011
@smasry smasry Reverse order fix when using function for ActiveRecord::QueryMethods …
…Fixes #1697
c751bb1
@jaw6 jaw6 added a commit that referenced this issue Aug 31, 2011
@jaw6 jaw6 Fixes bug in ActiveRecord::QueryMethods, #1697
Replace split on comma with a regexp that will reverse all ASC/DESC specifically
0df27c9
@jonleighton jonleighton added a commit that referenced this issue Sep 4, 2011
@jonleighton jonleighton Revert "Fixes bug in ActiveRecord::QueryMethods, #1697"
This reverts commit 0df27c9.

Reverted due to failing test, see #2845.
ef7e60f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.