From 90a0471af15808c12e32b9927cf7e37ccfaa0558 Mon Sep 17 00:00:00 2001 From: claudiob Date: Mon, 13 Oct 2014 16:39:07 -0700 Subject: Remove duplicate error message "Couldn't find..." This commit removes the duplication of the error message: > Couldn't find #{@klass.name} with [#{arel.where_sql}] introduced in #15791 by adding a private method `find_nth!` that deals with all the method like `first!` and `second!`. --- .../lib/active_record/relation/finder_methods.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index ed56369f86..c95ec2522b 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -138,7 +138,7 @@ module ActiveRecord # Same as +first+ but raises ActiveRecord::RecordNotFound if no record # is found. Note that first! accepts no arguments. def first! - first or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 0 end # Find the last record (or last N records if a parameter is supplied). @@ -187,7 +187,7 @@ module ActiveRecord # Same as +second+ but raises ActiveRecord::RecordNotFound if no record # is found. def second! - second or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 1 end # Find the third record. @@ -203,7 +203,7 @@ module ActiveRecord # Same as +third+ but raises ActiveRecord::RecordNotFound if no record # is found. def third! - third or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 2 end # Find the fourth record. @@ -219,7 +219,7 @@ module ActiveRecord # Same as +fourth+ but raises ActiveRecord::RecordNotFound if no record # is found. def fourth! - fourth or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 3 end # Find the fifth record. @@ -235,7 +235,7 @@ module ActiveRecord # Same as +fifth+ but raises ActiveRecord::RecordNotFound if no record # is found. def fifth! - fifth or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 4 end # Find the forty-second record. Also known as accessing "the reddit". @@ -251,7 +251,7 @@ module ActiveRecord # Same as +forty_two+ but raises ActiveRecord::RecordNotFound if no record # is found. def forty_two! - forty_two or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + find_nth! 41 end # Returns +true+ if a record exists in the table that matches the +id+ or @@ -489,6 +489,10 @@ module ActiveRecord end end + def find_nth!(index) + find_nth(index, offset_index) or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]") + end + def find_nth_with_limit(offset, limit) relation = if order_values.empty? && primary_key order(arel_table[primary_key].asc) -- cgit v1.2.3