jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
584 stars 228 forks source link

Descendant of AjaxDatatablesRails::Base loading triggers database connection #388

Closed Mathiou04 closed 1 year ago

Mathiou04 commented 3 years ago

Hi there,

First, thanks for all the great work with this gem!

I came here because last week I faced an issue when dropping and re-creating my test database.

Basically, when trying the re-create the DB after dropping using rake db:create, I got the following error: PG::ConnectionBad: FATAL: database "myapp_test" does not exist After investigating, the root cause was the interaction of a gem that needed to preload all models at boot and the fact that some models where inheriting AjaxDatatablesRails::Base which was trying to connect to the DB on load.

I fixed the issue by removing the other gem but I wonder if class load should be dependent on DB connection.

The issue was caused by the following code:

module AjaxDatatablesRails
  class Base

    class_attribute :db_adapter, default: ActiveRecord::Base.connection.adapter_name.downcase.to_sym

Calling ActiveRecord::Base.connection actually retrieve or establish a connection to the DB.

I checked ActiveRecord documentation a bit and wondered if it could simply be replaced by the following ActiveRecord::Base.connection_config[:adapter].to_sym. It is actually equivalent for some adapters but I am not sure it is for all of them.

Do you think it is worth opening a PR on this? All adapter names should be checked against those two methods to ensure it does not break anything.

Thanks again for putting this together, it is very useful when using datatables! Best

n-rodriguez commented 1 year ago

Fixed: https://github.com/jbox-web/ajax-datatables-rails/commit/5c0e274ff2aa750f4198d4a9bff851895908c9c1