Postgres サーバーが実行されている Mac Catalina 10.15.7 上の JRuby 9.3.3.0 で ActiveRecord を Postgres で使用しようとしています。@hmdne のアドバイスに従って、github.com/jruby/activerecord-jdbc-adapter の指示に従いました。エラーが表示されます:
NameError: 初期化されていない定数 ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG
これは私のコードです:
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")
スタック トレースにはエントリが含まれています
/Users/test/.rvm/gems/jruby-9.3.3.0/gems/activerecord-6.1.4.6/lib/active_record/connection_adapters/postgresql_adapter.rb:937
ライン937は
class MoneyDecoder < PG::SimpleDecoder # :nodoc:
つまり、ActiveRecord は PG でクラスをサブクラス化していますが、PG は JRuby でビルドされません。「gem install activerecord-postgresql-adapter」を実行しようとしましたが、その gem も「pg」gem に依存しているため、インストールは失敗します。
"require 'activerecord-jdbcpostgresql-adapter' if defined? JRUBY_VERSION" の代わりに、または require 'activerecord-jdbc-adapter' に加えて、行を試してみましたが、それでも同じ 'uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter: :PG'、および本質的に同じスタック トレース。
私はすべての宝石の最新バージョンを使用しています。ありがとう!!!
/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