<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -9,6 +9,7 @@ module ActiveRecord
         class &lt;&lt; klass
           alias_method_chain :reset_column_information, :sqlserver_cache_support
           alias_method_chain :add_order!, :sqlserver_unique_checking
+          alias_method_chain :add_limit!, :sqlserver_order_checking
         end
       end
       
@@ -45,6 +46,23 @@ module ActiveRecord
         
         private
         
+        def add_limit_with_sqlserver_order_checking!(sql, options, scope = :auto)
+          if connection.respond_to?(:sqlserver?)
+            scope = scope(:find) if :auto == scope
+            if scope
+              options = options.dup
+              scoped_order = scope[:order]
+              order = options[:order]
+              if order &amp;&amp; scoped_order
+                options[:order] = add_order_with_sqlserver_unique_checking!('', order, scope).gsub(/^ ORDER BY /,'')
+              elsif scoped_order
+                options[:order] = scoped_order
+              end
+            end
+          end
+          add_limit_without_sqlserver_order_checking!(sql, options, scope)
+        end
+        
         def add_order_with_sqlserver_unique_checking!(sql, order, scope = :auto)
           if connection.respond_to?(:sqlserver?)
             order_sql = ''</diff>
      <filename>lib/core_ext/active_record.rb</filename>
    </modified>
    <modified>
      <diff>@@ -159,6 +159,28 @@ class AdapterTestSqlserver &lt; ActiveRecord::TestCase
       end
 
     end
+
+    context &quot;for add_limit! within a scoped method call&quot; do
+      setup do
+        @connection.stubs(:select_value).with(regexp_matches(/TotalRows/)).returns '100000000'
+      end
+
+      should 'not add any ordering if the scope doesn\'t have an order' do
+        assert_equal 'SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 40 * FROM [developers]) AS tmp1) AS tmp2', add_limit!('SELECT * FROM [developers]', {:offset =&gt; 30, :limit =&gt; 10}, {})
+      end
+
+      should 'still add the default ordering if the scope doesn\'t have an order but the raw order option is there' do
+        assert_equal 'SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 40 * FROM [developers]) AS tmp1 ORDER BY [name] DESC) AS tmp2 ORDER BY [name]', add_limit!('SELECT * FROM [developers]', {:offset =&gt; 30, :limit =&gt; 10, :order =&gt; 'name'}, {})
+      end
+
+      should 'add scoped order options to the offset and limit sql' do
+        assert_equal 'SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 40 * FROM [developers]) AS tmp1 ORDER BY [id] DESC) AS tmp2 ORDER BY [id]', add_limit!('SELECT * FROM [developers]', {:offset =&gt; 30, :limit =&gt; 10}, {:order =&gt; 'id'})
+      end
+
+      should 'combine scoped order with raw order options in the offset and limit sql' do
+        assert_equal 'SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 40 * FROM [developers]) AS tmp1 ORDER BY [name] DESC, [id] DESC) AS tmp2 ORDER BY [name], [id]', add_limit!('SELECT * FROM [developers]', {:offset =&gt; 30, :limit =&gt; 10, :order =&gt; 'name'}, {:order =&gt; 'id'})
+      end
+    end
     
     context 'dealing with various orders SQL snippets' do
       
@@ -659,6 +681,11 @@ class AdapterTestSqlserver &lt; ActiveRecord::TestCase
     sql
   end
   
+  def add_limit!(sql, options, scope = :auto)
+    ActiveRecord::Base.send :add_limit!, sql, options, scope
+    sql
+  end
+
   def order_to_min_set(order)
     @connection.send :order_to_min_set, order
   end</diff>
      <filename>test/cases/adapter_test_sqlserver.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>9265961ea4de517447c8949d70cc784cb42b011b</id>
    </parent>
  </parents>
  <author>
    <name>Murray Steele</name>
    <email>muz@h-lame.com</email>
  </author>
  <url>http://github.com/rails-sqlserver/2000-2005-adapter/commit/a9f4d38220445a4309ad2291555b57ed01da793b</url>
  <id>a9f4d38220445a4309ad2291555b57ed01da793b</id>
  <committed-date>2009-06-18T06:33:47-07:00</committed-date>
  <authored-date>2009-06-11T07:01:58-07:00</authored-date>
  <message>Changing add_limit! in ActiveRecord::Base for SQLServer so that it passes through any scoped :order parameters.

We need these so that our limit and offset code can properly reverse and re-order the inner query and thus correctly paginate the results.

Signed-off-by: Murray Steele &lt;muz@h-lame.com&gt;</message>
  <tree>68bb64a45eec3a64f0664a82edf4d413f24507a6</tree>
  <committer>
    <name>Ken Collins</name>
    <email>ken@metaskills.net</email>
  </committer>
</commit>
