aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-11 14:52:51 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-11 15:07:24 +0000
commitf23adf0ed4bd596c9e7688455489a79f8b35eb3f (patch)
tree2afc6fd0a1277a036e19b207a7299572aebdfd77 /activerecord
parent0b6f514947c38bf7af2b7e98905a49a94832e5f6 (diff)
downloadrails-f23adf0ed4bd596c9e7688455489a79f8b35eb3f.tar.gz
rails-f23adf0ed4bd596c9e7688455489a79f8b35eb3f.tar.bz2
rails-f23adf0ed4bd596c9e7688455489a79f8b35eb3f.zip
Add tests for AssociationCollection#find_each and AssociationCollection#find_in_batches
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb39
2 files changed, 40 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 0fefec1216..f024f99a34 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -60,7 +60,7 @@ module ActiveRecord
@reflection.klass.find(*args)
end
end
-
+
# Fetches the first one using SQL if possible.
def first(*args)
if fetch_first_or_last_using_find?(args)
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index b7fa9d9d7c..99e4dc7e6e 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -219,6 +219,45 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
+ def test_find_each
+ firm = companies(:first_firm)
+
+ assert ! firm.clients.loaded?
+
+ assert_queries(3) do
+ firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
+ def test_find_each_with_conditions
+ firm = companies(:first_firm)
+
+ assert_queries(2) do
+ firm.clients.find_each(:batch_size => 1, :conditions => {:name => "Microsoft"}) do |c|
+ assert_equal firm.id, c.firm_id
+ assert_equal "Microsoft", c.name
+ end
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
+ def test_find_in_batches
+ firm = companies(:first_firm)
+
+ assert ! firm.clients.loaded?
+
+ assert_queries(2) do
+ firm.clients.find_in_batches(:batch_size => 2) do |clients|
+ clients.each {|c| assert_equal firm.id, c.firm_id }
+ end
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
def test_find_all_sanitized
firm = Firm.find(:first)
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")