尝试在 Mac Catalina 10.15.7 上的 JRuby 9.3.3.0 上使用 ActiveRecord 和 Postgres,同时运行 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 行是
类 MoneyDecoder < PG::SimpleDecoder # :nodoc:
所以 ActiveRecord 是 PG 中的一个类的子类化,但 PG 不会建立在 JRuby 之上。尝试执行 'gem install activerecord-postgresql-adapter' 但安装失败,因为该 gem 还依赖于 'pg' gem。
尝试了行“需要'activerecord-jdbcpostgresql-adapter'如果定义?JRUBY_VERSION”代替或除了需要'activerecord-jdbc-adapter',但我仍然得到相同的'未初始化常量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