0

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
  • 2
    Maybe you can use [jruby-pg](https://github.com/headius/jruby-pg)? It's written by Charles Nutter, the primary maintainer of JRuby. – Jared Beck Feb 14 '22 at 00:49
  • 1
    You can use JDBC instead of PG. You certainly tried that, but you will need to set things up a little differently: https://github.com/jruby/activerecord-jdbc-adapter I would recommend you to take a look in your Gemfile.lock file to find what actually tries to load the `pg` gem – hmdne Feb 14 '22 at 01:37
  • Thanks to both of you for your help! @JaredBeck I tried jruby-pg a while back. I had some trouble getting it working for me (I forget exactly what), then I noticed it hasn't had a commit since 2017, so I wondered if the JDBC gems (one of which had a commit 5 days ago) might be a better supported choice for the long run. – Wayne Vucenic Feb 15 '22 at 00:02
  • @hmdne I'm brand new to postgres, and I finally realized that I didn't have the server running or configured correctly. On the (slim?) chance that this is partially contributing to this problem, I'm going to make sure I've got this correct first. I don't have much time to work on this, so I'm not quite there yet. I'll put a note here when I make some progress. Thanks for all your work on Opal! I'm looking forward to someday working with Opal and glimmer-swt-opal – Wayne Vucenic Feb 15 '22 at 00:03
  • 1
    @hmdne Re: the `Gemfile.lock`, [`pg` is not a hard runtime dependency of ActiveRecord](https://github.com/rails/rails/blob/60f274483354452aa0e651c3e0365d19d3e612c6/activerecord/activerecord.gemspec#L38). AR will use it if present, otherwise it will raise the `LoadError` that Wayne encountered. – Jared Beck Feb 15 '22 at 20:48
  • I've edited my question after incorporating the above comments. – Wayne Vucenic Feb 21 '22 at 01:29

0 Answers0