@@ -10,7 +10,6 @@ module ActiveRecord
10
10
lambda { |klass | !!defined? ( ::ActiveRecord ::Base ) && klass . ancestors . include? ( ::ActiveRecord ::Base ) }
11
11
12
12
module Records
13
-
14
13
# Returns an `ActiveRecord::Relation` instance
15
14
#
16
15
def records
@@ -21,7 +20,11 @@ def records
21
20
#
22
21
sql_records . instance_exec ( response [ 'hits' ] [ 'hits' ] ) do |hits |
23
22
define_singleton_method :to_a do
24
- self . load
23
+ if ::ActiveRecord . respond_to? ( :version ) && ::ActiveRecord . version . to_s > '4'
24
+ self . load
25
+ else
26
+ self . __send__ ( :exec_queries )
27
+ end
25
28
@records . sort_by { |record | hits . index { |hit | hit [ '_id' ] . to_s == record . id . to_s } }
26
29
end
27
30
end
@@ -43,7 +46,14 @@ def order(*args)
43
46
# Redefine the `to_a` method to the original one
44
47
#
45
48
sql_records . instance_exec do
46
- define_singleton_method ( :to_a ) { self . load ; @records }
49
+ define_singleton_method ( :to_a ) do
50
+ if ::ActiveRecord . respond_to? ( :version ) && ::ActiveRecord . version . to_s > '4'
51
+ self . load
52
+ else
53
+ self . __send__ ( :exec_queries )
54
+ end
55
+ @records
56
+ end
47
57
end
48
58
49
59
sql_records
@@ -59,9 +69,9 @@ module Callbacks
59
69
#
60
70
def self . included ( base )
61
71
base . class_eval do
62
- after_commit lambda { __elasticsearch__ . index_document } , on : [ :create ]
63
- after_commit lambda { __elasticsearch__ . update_document } , on : [ :update ]
64
- after_commit lambda { __elasticsearch__ . delete_document } , on : [ :destroy ]
72
+ after_commit lambda { __elasticsearch__ . index_document } , on : :create
73
+ after_commit lambda { __elasticsearch__ . update_document } , on : :update
74
+ after_commit lambda { __elasticsearch__ . delete_document } , on : :destroy
65
75
end
66
76
end
67
77
end
0 commit comments