aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/finder_methods.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb59
1 files changed, 20 insertions, 39 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 7bf84c2f15..d46b4e0683 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -117,7 +117,7 @@ module ActiveRecord
#
def first(limit = nil)
if limit
- find_nth_with_limit_and_offset(0, limit, offset: offset_index)
+ find_nth_with_limit(0, limit)
else
find_nth 0
end
@@ -348,15 +348,17 @@ module ActiveRecord
def raise_record_not_found_exception!(ids, result_size, expected_size) #:nodoc:
conditions = arel.where_sql(@klass.arel_engine)
conditions = " [#{conditions}]" if conditions
+ name = @klass.name
if Array(ids).size == 1
- error = "Couldn't find #{@klass.name} with '#{primary_key}'=#{ids}#{conditions}"
+ error = "Couldn't find #{name} with '#{primary_key}'=#{ids}#{conditions}"
+ raise RecordNotFound.new(error, name, primary_key, ids)
else
- error = "Couldn't find all #{@klass.name.pluralize} with '#{primary_key}': "
+ error = "Couldn't find all #{name.pluralize} with '#{primary_key}': "
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})"
- end
- raise RecordNotFound, error
+ raise RecordNotFound, error
+ end
end
private
@@ -426,7 +428,7 @@ module ActiveRecord
arel = relation.arel
id_rows = @klass.connection.select_all(arel, "SQL", relation.bound_attributes)
- id_rows.map {|row| row[primary_key]}
+ id_rows.map { |row| row[primary_key] }
end
def using_limitable_reflections?(reflections)
@@ -520,22 +522,8 @@ module ActiveRecord
end
end
- def find_nth(index, offset = nil)
- # TODO: once the offset argument is removed we rely on offset_index
- # within find_nth_with_limit, rather than pass it in via
- # find_nth_with_limit_and_offset
- if offset
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
- Passing an offset argument to find_nth is deprecated,
- please use Relation#offset instead.
- MSG
- end
- if loaded?
- records[index]
- else
- offset ||= offset_index
- @offsets[offset + index] ||= find_nth_with_limit_and_offset(index, 1, offset: offset).first
- end
+ def find_nth(index)
+ @offsets[offset_index + index] ||= find_nth_with_limit(index, 1).first
end
def find_nth!(index)
@@ -543,16 +531,18 @@ module ActiveRecord
end
def find_nth_with_limit(index, limit)
- # TODO: once the offset argument is removed from find_nth,
- # find_nth_with_limit_and_offset can be merged into this method.
- relation = if order_values.empty? && primary_key
- order(arel_attribute(primary_key).asc)
+ if loaded?
+ records[index, limit]
else
- self
- end
+ relation = if order_values.empty? && primary_key
+ order(arel_attribute(primary_key).asc)
+ else
+ self
+ end
- relation = relation.offset(index) unless index.zero?
- relation.limit(limit).to_a
+ relation = relation.offset(offset_index + index) unless index.zero?
+ relation.limit(limit).to_a
+ end
end
def find_nth_from_last(index)
@@ -576,15 +566,6 @@ module ActiveRecord
private
- def find_nth_with_limit_and_offset(index, limit, offset:) # :nodoc:
- if loaded?
- records[index, limit]
- else
- index += offset
- find_nth_with_limit(index, limit)
- end
- end
-
def find_last(limit)
limit ? records.last(limit) : records.last
end