drnic/composite_primary_keys — pull request #133 by tbeauvais (2012-12-05 16:03:06+00:00)
Title: Fix for CompositePrimaryKeys::ActiveRecord::QueryMethods reverse_order method when default scope is using :order

I have created a fix for CompositePrimaryKeys::ActiveRecord::QueryMethods reverse_order() method when the model contains a default scope that is using :order.
The issue is that the active record except() method does not skip if the :order is specified in a default scope.
The except() method does an explicit copy of the default_scope before the actual processing.

**Before fix** (note the extra sort_order on ORDER BY)
reverse_order SELECT `addresses`.* FROM `addresses` ORDER BY sort_order, sort_order DESC
Address Load (0.2ms) SELECT `addresses`.* FROM `addresses` ORDER BY sort_order, sort_order DESC LIMIT 1

except(:order).order(Arel.sql(order))

**After patch**
reverse_order SELECT `addresses`.* FROM `addresses` ORDER BY sort_order, sort_order DESC
Address Load (0.3ms) SELECT `addresses`.* FROM `addresses` ORDER BY sort_order DESC LIMIT 1

reorder(Arel.sql(order))

I have added a new test for this in test_find.rb. I'm using MySQL for my testing.

I also noticed that some DB fields were missing for the tariffs table in the mysql.sql file that are used in touch_test.rb.
This test was failing, so I added the missing fields in the mysql.sql file.