aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-10-13 20:50:12 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-10-13 20:50:12 -0300
commitc9504753504fbcbf68f870376f218cdd17cae0a0 (patch)
tree5bdef94e82492817fa7ee9c28a6538f4f9f9e451 /activerecord
parent2a0d97bc89fc230241453cbbf55621309e1f3ac6 (diff)
parent90a0471af15808c12e32b9927cf7e37ccfaa0558 (diff)
downloadrails-c9504753504fbcbf68f870376f218cdd17cae0a0.tar.gz
rails-c9504753504fbcbf68f870376f218cdd17cae0a0.tar.bz2
rails-c9504753504fbcbf68f870376f218cdd17cae0a0.zip
Merge pull request #17251 from claudiob/remove-duplicate-exception
Remove duplicate error message "Couldn't find..."
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb16
1 files changed, 10 insertions, 6 deletions
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 <tt>ActiveRecord::RecordNotFound</tt> if no record
# is found. Note that <tt>first!</tt> 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 <tt>ActiveRecord::RecordNotFound</tt> 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 <tt>ActiveRecord::RecordNotFound</tt> 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 <tt>ActiveRecord::RecordNotFound</tt> 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 <tt>ActiveRecord::RecordNotFound</tt> 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 <tt>ActiveRecord::RecordNotFound</tt> 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)