From 76b35e795ce66ae5d0717fb8e73fc16f38d9e5a3 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Fri, 28 May 2010 14:00:24 -0300 Subject: [PATCH] Refactor expected result size check into method. [#4720 state:resolved] --- .../lib/active_record/relation/finder_methods.rb | 49 ++++++++++--------- 1 files changed, 26 insertions(+), 23 deletions(-) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 7a0c9dc..9f56c27 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -292,29 +292,7 @@ module ActiveRecord def find_some(ids) result = where(primary_key.in(ids)).all - - expected_size = - if @limit_value && ids.size > @limit_value - @limit_value - else - ids.size - end - - # 11 ids with limit 3, offset 9 should give 2 results. - if @offset_value && (ids.size - @offset_value < expected_size) - expected_size = ids.size - @offset_value - end - - if result.size == expected_size - result - else - conditions = arel.send(:where_clauses).join(', ') - conditions = " [WHERE #{conditions}]" if conditions.present? - - error = "Couldn't find all #{@klass.name.pluralize} with IDs " - error << "(#{ids.join(", ")})#{conditions} (found #{result.size} results, but was looking for #{expected_size})" - raise RecordNotFound, error - end + validate_expected_size!(ids, result) end def find_first @@ -342,5 +320,30 @@ module ActiveRecord reflections.collect(&:collection?).length.zero? end + def validate_expected_size!(ids, result) + expected_size = + if @limit_value && ids.size > @limit_value + @limit_value + else + ids.size + end + + # 11 ids with limit 3, offset 9 should give 2 results. + if @offset_value && (ids.size - @offset_value < expected_size) + expected_size = ids.size - @offset_value + end + + if result.size == expected_size + result + else + conditions = arel.send(:where_clauses).join(', ') + conditions = " [WHERE #{conditions}]" if conditions.present? + + error = "Couldn't find all #{@klass.name.pluralize} with IDs " + error << "(#{ids.join(", ")})#{conditions} (found #{result.size} results, but was looking for #{expected_size})" + raise RecordNotFound, error + end + end + end end -- 1.7.1