diff options
author | Matthew Draper <matthew@trebex.net> | 2015-12-18 16:38:47 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2015-12-18 16:42:30 +1030 |
commit | 2750b1b4177db03a0848432e9413a0b3acec38c8 (patch) | |
tree | 5ff0b9a523e06e4bf11d93a3b3d7514214c259f8 /activerecord/lib/active_record/relation/finder_methods.rb | |
parent | 503255d21bb80d2d22fe2315ff9b7688cb3342a7 (diff) | |
parent | 04309aee82468fa4c4b3d92a533e84a96533f236 (diff) | |
download | rails-2750b1b4177db03a0848432e9413a0b3acec38c8.tar.gz rails-2750b1b4177db03a0848432e9413a0b3acec38c8.tar.bz2 rails-2750b1b4177db03a0848432e9413a0b3acec38c8.zip |
Merge pull request #22653 from matthewd/find_array_ordered
ActiveRecord::Base#find(array) returning result in the same order as the array passed
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 435cef901b..19244bcf95 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -442,6 +442,8 @@ module ActiveRecord end def find_some(ids) + return find_some_ordered(ids) unless order_values.present? + result = where(primary_key => ids).to_a expected_size = @@ -463,6 +465,21 @@ module ActiveRecord end end + def find_some_ordered(ids) + ids = ids.slice(offset_value || 0, limit_value || ids.size) || [] + + result = except(:limit, :offset).where(primary_key => ids).to_a + + if result.size == ids.size + pk_type = @klass.type_for_attribute(primary_key) + + records_by_id = result.index_by(&:id) + ids.map { |id| records_by_id.fetch(pk_type.cast(id)) } + else + raise_record_not_found_exception!(ids, result.size, ids.size) + end + end + def find_take if loaded? @records.first |