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
585 stars 228 forks source link

DataTables in Rails 6 and JRuby wants :postgresql adapter #371

Closed jeffkirk1 closed 3 years ago

jeffkirk1 commented 3 years ago

I have to use JRuby for an application I'm writing that needs to use JDBC connectors to a variety of different databases. There is no native "pg" gem for JRuby aside from an (apparently) no longer maintained gem named "jruby-pg". I was unable to get the latest version of Rails 6 (also a hard requirement) to run with this old jruby-pg gem. Rails expects version 1.18 or better.

When trying your instructions to use the generic adapter, uncommenting

config.db_adapter = Rails.configuration.database_configuration[Rails.env]['adapter'].to_sym

This resolves to :postgresql and queries to the controller for JSON output generate an error like this (home directory path and the app name obfuscated for privacy):

no such file to load -- $HOME/.rvm/gems/jruby-9.2.13.0@$APP_NAME/gems/ajax-datatables-rails-1.2.0/lib/ajax-datatables-rails/orm

There IS a file in this directory: active_record.rb

The extracted source of the message is:

class TestDatatable < AjaxDatatablesRails::ActiveRecord

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format
    @view_columns ||= {

I have pretty much identical code in a C Ruby version of another app using the pg gem, and it does not exhibit this problem. Happy to provide any needed info about this app to see if we can get it working with JRuby. Otherwise I have to load all of the thousands of results in my views to be able to use DataTables search and filtering functions, which would be burdensome.

n-rodriguez commented 3 years ago

You must use https://github.com/jruby/activerecord-jdbc-adapter

jeffkirk1 commented 3 years ago

I am using the ActiveRecord JDBC adapter according to the instructions on their site. Here is my gem list:

activerecord-jdbcpostgresql-adapter (60.2 java)
jdbc-postgres (42.2.14)

Connections fo the PostgreSQL database are defined in database.yml (actual values obfuscated):

development:
  adapter:  postgresql
  encoding: unicode
  database: DBNAME
  username: USERNAME
  password: PASSWORD

I am using JRuby 9.2.13.0 installed by RVM. Issue reproducible on macOS 10.15.6 and Ubuntu LTS 20.04.1.

jeffkirk1 commented 3 years ago

Sorry, apparently my first post was worded so it looked like I had only tried the jruby-pg gem. I meant to say that I couldn't get the ajax-datatables-rails gem to connect using the JDBC connection, which is my only option.

jeffkirk1 commented 3 years ago

I created a standalone app repo which replicates this problem. git@github.com:jeffkirk1/datatables_on_jruby.git

Install JRuby and gemset with RVM according to the .ruby-version and .ruby-gemset files. Details on the postgresql database connection are all in config/database.yml. 10,000 Test records can be created by running "rake seed:test" before starting the Rails server. The root path is to the tests#index view. The Javascript to load the DataTable is embedded in the index.html.erb page.

n-rodriguez commented 3 years ago

no such file to load -- $HOME/.rvm/gems/jruby-9.2.13.0@$APP_NAME/gems/ajax-datatables-rails-1.2.0/lib/ajax-datatables-rails/orm

This is a JRuby / Zeitwerk issue. The file gems/ajax-datatables-rails-1.2.0/lib/ajax-datatables-rails/orm.rb must exist. This is now fixed. Can you please test with master branch?

jeffkirk1 commented 3 years ago

Unfortunately using the gem from the master branch source code now generates a fatal error when starting Rails server or console in the test project. The DB adapter defined in this line no longer resolves:

config.db_adapter = Rails.configuration.database_configuration[Rails.env]['adapter'].to_sym

This resolves to :postgresql

I switched it back to the official gem and it starts but the error is still present. I also tried replacing the code above with

config.db_adapter = :postgresql

but the effect was the same.

jkirk@imac datatables_on_jruby % rails c NoMethodError: undefined method `db_adapter=' for #

at /Users/jkirk/Documents/git/external/datatables_on_jruby/config/initializers/ajax_datatables_rails.rb:5 configure at /Users/jkirk/.rvm/gems/jruby-9.2.13.0@datatables_on_jruby/bundler/gems/ajax-datatables-rails-6e2b3a3b0924/lib/ajax-datatables-rails.rb:23
at /Users/jkirk/Documents/git/external/datatables_on_jruby/config/initializers/ajax_datatables_rails.rb:3 load at org/jruby/RubyKernel.java:1016 load at /Users/jkirk/.rvm/gems/jruby-9.2.13.0@datatables_on_jruby/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318
jeffkirk1 commented 3 years ago

FYI, same thing happens with jruby 9.2.14.0 and Rails 6.1.0.

jeffkirk1 commented 3 years ago

Also FYI

jkirk@imac datatables_on_jruby % gem list | grep activerecord activerecord (6.0.3.4) activerecord-jdbc-adapter (60.3 java) activerecord-jdbcpostgresql-adapter (60.3 java)

n-rodriguez commented 3 years ago

I switched it back to the official gem and it starts but the error is still present. I also tried replacing the code above with

config.db_adapter = :postgresql

but the effect was the same.

Yeah right. The master branch has changed last night and this file config/initializers/ajax_datatables_rails.rb is now useless.

See the migration guide

jeffkirk1 commented 3 years ago

Thanks, it does work now, and the instructions for installing the gem under yarn were accurate. I appreciate your replying during the holidays. Keep up the good work! This tool is freaking essential. :-)