Skip to content

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

Closed
@Mathiou04

Description

@Mathiou04

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions