Report abuse

--- lib/active_record/associations.rb	2008-07-15 23:13:47.000000000 -0500
+++ /Users/cjbottaro/associations.rb	2008-07-18 15:47:21.000000000 -0500
@@ -1455,7 +1455,8 @@
         end

         def construct_finder_sql_for_association_limiting(options, join_dependency)
-          scope       = scope(:find)
+          scope       = scope(:find) || {}
+          order       = [options[:order], scope[:order]].compact.join(', ')

           # Only join tables referenced in order or conditions since this is particularly slow on the pre-query.
           tables_from_conditions = conditions_tables(options)
@@ -1468,7 +1469,7 @@
           is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
           sql = "SELECT "
           if is_distinct
-            sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", options[:order])
+            sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", order)
           else
             sql << primary_key
           end
@@ -1482,8 +1483,8 @@
           add_conditions!(sql, options[:conditions], scope)
           add_group!(sql, options[:group], scope)

-          if options[:order] && is_distinct
-            connection.add_order_by_for_association_limiting!(sql, options)
+          if order && is_distinct
+            connection.add_order_by_for_association_limiting!(sql, :order => order)
           else
             add_order!(sql, options[:order], scope)
           end