Skip to content

Commit c9c4d2f

Browse files
Savater Sebastienkarmi
Savater Sebastien
authored andcommitted
[MODEL] Fixed records sorting with ActiveRecord 5.x
Closes elastic#618
1 parent 8f8761a commit c9c4d2f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def records
2626
# by redefining `to_a`, unless the user has called `order()`
2727
#
2828
sql_records.instance_exec(response.response['hits']['hits']) do |hits|
29-
define_singleton_method :to_a do
29+
ar_records_method_name = :to_a
30+
ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5
31+
32+
define_singleton_method(ar_records_method_name) do
3033
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
3134
self.load
3235
else

elasticsearch-model/test/integration/active_record_basic_test.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ def as_indexed_json(options = {})
104104
end
105105

106106
should "preserve the search results order for records" do
107-
response = Article.search('title:code')
107+
response = Article.search query: { match: { title: 'code' }}, sort: { clicks: :desc }
108+
109+
assert_equal response.records[0].clicks, 3
110+
assert_equal response.records[1].clicks, 2
108111

109112
response.records.each_with_hit do |r, h|
110113
assert_equal h._id, r.id.to_s

0 commit comments

Comments
 (0)