flyerhzm / bullet

help to kill N+1 queries and unused eager loading
MIT License
7.02k stars 425 forks source link

I'm using Mongoid but getting "Gem Load Error is: uninitialized constant ActiveRecord::VERSION" #409

Open rgaufman opened 6 years ago

rgaufman commented 6 years ago

Hi there, I'm using rails (5.2.0), bullet (5.7.5) with mongoid (6.4.1) - I don't have (or want) ActiveRecord.

When I run rails c, I get this:

✗ rails c
Traceback (most recent call last):
    18: from bin/rails:4:in `<main>'
    17: from bin/rails:4:in `require'
    16: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<top (required)>'
    15: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke'
    14: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform'
    13: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
    12: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    11: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
    10: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/console/console_command.rb:95:in `perform'
     9: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/actions.rb:17:in `require_application_and_environment!'
     8: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/actions.rb:17:in `require'
     7: from /Users/hackeron/Dropbox/Development/Xanview/timeline/config/application.rb:26:in `<top (required)>'
     6: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler.rb:114:in `require'
     5: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:65:in `require'
     4: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:65:in `each'
     3: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:76:in `block in require'
     2: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:76:in `each'
     1: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:80:in `block (2 levels) in require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:84:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'bullet'. (Bundler::GemRequireError)
Gem Load Error is: uninitialized constant ActiveRecord::VERSION
Backtrace for gem load error is:
/usr/local/lib/ruby/gems/2.5.0/gems/bullet-5.7.5/lib/bullet/dependency.rb:52:in `active_record4?'
/usr/local/lib/ruby/gems/2.5.0/gems/bullet-5.7.5/lib/bullet/dependency.rb:60:in `active_record40?'
/usr/local/lib/ruby/gems/2.5.0/gems/bullet-5.7.5/lib/bullet/dependency.rb:19:in `active_record_version'
/usr/local/lib/ruby/gems/2.5.0/gems/bullet-5.7.5/lib/bullet.rb:14:in `<module:Bullet>'
/usr/local/lib/ruby/gems/2.5.0/gems/bullet-5.7.5/lib/bullet.rb:11:in `<top (required)>'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:81:in `require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:81:in `block (2 levels) in require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:76:in `each'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:76:in `block in require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:65:in `each'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler/runtime.rb:65:in `require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/bundler.rb:114:in `require'

I have this in my Gemfile:

group :test, :development do
  gem 'bullet'  # Detect and show slow queries
end

And this in my development.rb

  # Detect slow queries
  config.after_initialize do
    Bullet.enable = false # temporarily disabled
    Bullet.alert = true
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
    Bullet.add_footer = true
    Bullet.raise = false
    Bullet.unused_eager_loading_enable = false
  end if defined?(Bullet)

Any ideas?

rgaufman commented 6 years ago

It seems the issue is with Rails 5.2.0 because of ActiveStorage, hmm. In my spec_helper when I add the following, the problem goes away:

# Remove the ActiveRecord constant, because it is autloaded by
# ActiveStorage and not needed for our application. The presence
# of the ActiveRecord constant causes rspec-rails to include
# extra fixture support, which results in:
#
#   ActiveRecord::ConnectionNotEstablished:
#     No connection pool with 'primary' found.
#
Object.send(:remove_const, :ActiveRecord)

But I'm still seeing the problem running in development. Any ideas?

rileys commented 3 years ago

I added this at the beginning of application.rb, after requires and seemed to fix this in development.