Skip to content

Commit 0e7acd8

Browse files
committed
[MODEL] Override ActiveRecord::Base.inspect to prevent ActiveRecord::ConnectionNotEstablished errors
Rails 4 prevents these errors by default, see: https://github.com/rails/rails/blob/v4.0.2/activerecord/lib/active_record/core.rb#L108-L122 Related: elastic#6
1 parent ef6bc35 commit 0e7acd8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
require 'elasticsearch/model/support/forwardable'
88

9+
require 'elasticsearch/model/version'
10+
911
require 'elasticsearch/model/client'
1012

1113
require 'elasticsearch/model/adapter'
@@ -29,7 +31,7 @@
2931
require 'elasticsearch/model/response/records'
3032
require 'elasticsearch/model/response/pagination'
3133

32-
require 'elasticsearch/model/version'
34+
require 'elasticsearch/model/ext/active_record'
3335

3436
if defined?(::Kaminari)
3537
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Prevent `MyModel.inspect` failing with `ActiveRecord::ConnectionNotEstablished`
2+
# (triggered by elasticsearch-model/lib/elasticsearch/model.rb:79:in `included')
3+
#
4+
ActiveRecord::Base.instance_eval do
5+
class << self
6+
def inspect_with_rescue
7+
inspect_without_rescue
8+
rescue ActiveRecord::ConnectionNotEstablished
9+
"#{self}(no database connection)"
10+
end
11+
12+
alias_method_chain :inspect, :rescue
13+
end
14+
end if defined?(ActiveRecord) && ActiveRecord::VERSION::STRING < '4'

0 commit comments

Comments
 (0)