public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Implement count limit/offset support for has_many associations

[#348 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Tarmo Tänav (author)
Tue Aug 26 09:29:16 -0700 2008
jeremy (committer)
Wed Aug 27 23:32:43 -0700 2008
commit  367942d93bbb99aa2c2f8dc3900cb7c3f2ba4c65
tree    eae0e36f8177e99195865d873665f853106d82db
parent  0ed29df6fa704349fd0af0d9521581d6a8eb109c
...
14
15
16
17
 
 
 
 
 
 
 
 
 
 
18
19
20
...
14
15
16
 
17
18
19
20
21
22
23
24
25
26
27
28
29
0
@@ -14,7 +14,16 @@ module ActiveRecord
0
             @finder_sql + " AND (#{sanitize_sql(options[:conditions])})"
0
           options[:include] ||= @reflection.options[:include]
0
 
0
-          @reflection.klass.count(column_name, options)
0
+          value = @reflection.klass.count(column_name, options)
0
+
0
+          limit  = @reflection.options[:limit]
0
+          offset = @reflection.options[:offset]
0
+
0
+          if limit || offset
0
+            [ [value - offset.to_i, 0].max, limit.to_i ].min
0
+          else
0
+            value
0
+          end
0
         end
0
       end
0
 
...
48
49
50
 
 
 
 
 
 
51
52
53
...
48
49
50
51
52
53
54
55
56
57
58
59
0
@@ -48,6 +48,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal 2, Firm.find(:first).plain_clients.count(:name)
0
   end
0
 
0
+  def test_counting_with_association_limit
0
+    firm = companies(:first_firm)
0
+    assert_equal firm.limited_clients.length, firm.limited_clients.size
0
+    assert_equal firm.limited_clients.length, firm.limited_clients.count
0
+  end
0
+
0
   def test_finding
0
     assert_equal 2, Firm.find(:first).clients.length
0
   end

Comments