rails/activemodel-serializers-xml

ActiveRecord is autoloaded in Rails projects that don't use it

Closed this issue · 3 comments

This is an ActiveModel library, not an ActiveRecord library, right?

It looks like this library automatically extends ActiveRecord. This causes ActiveRecord to be autoloaded in rails, even in applications that make no use of it (apps that use other ORMs that make use of ActiveModel). That causes a string of other libraries (such as FactoryGirl) to load their AR code, which ultimately causes AR to try to establish a database connection.

I'd like to see this library check whether ActiveRecord is actually loaded before extending it to avoid causing a mess in Rails apps that don't use it.

This is causing me pain now because now rspec-rails checks for ActiveRecord and assumes the connection will be established:

     Failure/Error: raise ConnectionNotEstablished, "No connection pool with id #{spec_name} found." unless pool

     ActiveRecord::ConnectionNotEstablished:
       No connection pool with id primary found.
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `retrieve_connection'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:91:in `connection'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:516:in `create_fixtures'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:1015:in `load_fixtures'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:988:in `setup_fixtures'
     # ./.bundle/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:852:in `before_setup'

Is there any way we could address this?

It is already. In only patch ActiveRecord if it is already loaded.