JRuby: How to use the ActiveRecord postgresql adapter without `pg`, which is written in C?

383 Views Asked by At

Trying to use ActiveRecord with Postgres on JRuby 9.3.3.0 on Mac Catalina 10.15.7, with the Postgres server running. Followed the instructions at github.com/jruby/activerecord-jdbc-adapter per @hmdne advice. I see the error:

NameError: uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG

This is my code:

require 'active_record'
require 'activerecord-jdbc-adapter' if defined? JRUBY_VERSION

ActiveRecord::Base.establish_connection(
  adapter: "postgresql",
  host: "localhost",
  database: "test",
  username: "postgres",
  password: "password")

The stack trace contains the entry

/Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:937

Line 937 is

class MoneyDecoder < PG::SimpleDecoder # :nodoc:

So ActiveRecord is subclassing a class in PG, but PG won't build on JRuby. Tried doing 'gem install activerecord-postgresql-adapter' but the install fails because that gem also depends on the 'pg' gem.

Tried the line "require 'activerecord-jdbcpostgresql-adapter' if defined? JRUBY_VERSION" either instead of or in addition to the require 'activerecord-jdbc-adapter' but I still get the same 'uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG', and essentially the same stack trace.

I'm using the latest versions of all the gems. Thanks!!!

/bin/zsh -c "bash -c '/Users/test/.rvm/bin/rvm jruby-9.3.3.0 do /Users/test/.rvm/rubies/jruby-9.3.3.0/bin/jruby /Users/test/RubymineProjects/glimmer/TaskTimer2/lib/testSO.rb'"
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG
        load_missing_constant at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activesupport-6.1.4.6/lib/active_support/dependencies.rb:603
                const_missing at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activesupport-6.1.4.6/lib/active_support/dependencies.rb:213
    <class:PostgreSQLAdapter> at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:937
  <module:ConnectionAdapters> at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:73
        <module:ActiveRecord> at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:45
                       <main> at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:22
                      require at org/jruby/RubyKernel.java:1017
                      require at /Users/test/.rvm/rubies/jruby-9.3.3.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:85
                      require at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activesupport-6.1.4.6/lib/active_support/dependencies.rb:332
              load_dependency at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activesupport-6.1.4.6/lib/active_support/dependencies.rb:299
                      require at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activesupport-6.1.4.6/lib/active_support/dependencies.rb:332
          resolve_pool_config at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:1205
         establish_connection at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:1046
         establish_connection at /Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_handling.rb:52
                       <main> at /Users/test/RubymineProjects/glimmer/TaskTimer2/lib/testSO.rb:4

Process finished with exit code 1
0

There are 0 best solutions below