Skip to content

Commit 0143aa7

Browse files
jazzytomatoestolfo
authored andcommittedSep 4, 2018
Fix sort order on ActiveRecord >= 5. re issue elastic#546 (elastic#831)
1 parent 19e953a commit 0143aa7

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed
 

‎elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ def order(*args)
5656
# Redefine the `to_a` method to the original one
5757
#
5858
sql_records.instance_exec do
59-
define_singleton_method(:to_a) do
59+
ar_records_method_name = :to_a
60+
ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5
61+
62+
define_singleton_method(ar_records_method_name) do
6063
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
6164
self.load
6265
else

‎elasticsearch-model/test/integration/active_record_basic_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ def as_indexed_json(options = {})
223223

224224
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
225225
assert_equal 'Testing Coding', response.records.order(title: :desc).first.title
226+
assert_equal 'Testing Coding', response.records.order(title: :desc)[0].title
226227
else
227228
assert_equal 'Testing Coding', response.records.order('title DESC').first.title
229+
assert_equal 'Testing Coding', response.records.order('title DESC')[0].title
228230
end
229231
end
230232

0 commit comments

Comments
 (0)