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:21 -0700 2008
commit  96c6fe084228d570dad80e3100830edb2bc0448d
tree    f573cc36df43094cb2b9f7a5cf6ba3406e5edb24
parent  13671cc565aad2327f81a29789154b829ceeda04
...
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