通过Ruby Active Record JDBC访问Sql Server

时间:2017-08-24 19:43:50

标签: sql-server ruby activerecord jdbc jruby

我一直在尝试使用Ruby中的Active Record(特别是JRuby)连接到远程JDBC / SqlServer数据库。这是我的代码:

require 'activerecord'
require 'activerecord-jdbc-adapter'

ActiveRecord::Base.establish_connection(
                      :adapter => 'sqlserver',
                      :username => '<username>',
                      :password => '<password>',
                      :database => '<database_name>',
                      :url => '<database_url>',
)
@connection = ActiveRecord::Base.connection

puts @connection

我还把'jdbc'作为适配器,这也没用。

这是我得到的错误的缩写版本:

NameError: cannot load Java class com.microsoft.sqlserver.jdbc.SQLServerDriver
                  for_name at org/jruby/javasupport/JavaClass.java:286
           get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
      block in java_import at uri:classloader:/jruby/java/core_ext/object.rb:49
                       map at org/jruby/RubyArray.java:2486
               java_import at uri:classloader:/jruby/java/core_ext/object.rb:36
     block in driver_class at C:/jruby-9.1.12.0/lib/ruby/gems/shared/gems/activerecord-jdbc-adapter-1.3.23/lib/arjdbc/jdbc/driver.rb:24
               module_eval at org/jruby/RubyModule.java:2833
     block in driver_class at C:/jruby-9.1.12.0/lib/ruby/gems/shared/gems/activerecord-jdbc-adapter-1.3.23/lib/arjdbc/jdbc/driver.rb:23
              synchronized at org/jruby/javasupport/JavaObject.java:257

这不是一个特定的问题,而是更多的是我只是不确定从哪里开始。由于服务器上的JDBC,在纯Ruby中使用'activerecord-sqlserver-adapter'不起作用。 (我可能在这里滥用这些术语,我对数据库的工作原理并不熟悉。)

然而,jdbc / sqlserver gems似乎没有得到很好的支持或过时。可能没有银弹,但任何方向都会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

你还需要require 'active_record',不是吗?

我不确定你需要require 'arel'。这是由active_record实现的低级sql builder / formatter。

require 'active_record'
require 'activerecord-jdbc-adapter'

ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.establish_connection(
  :adapter => "sqlserver",
  :host => "SqlServerHostName",
  :database => "HostDbName"
)