如何使用 Oracle 配置 Ruby on Rails?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/764887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:05:43  来源:igfitidea点击:

How to configure Ruby on Rails with Oracle?

ruby-on-railsrubyoracle

提问by Mark Harrison

There's several pages on the web that discuss this, but most are out of date or inaccurate in some way.

网络上有几个页面对此进行了讨论,但大多数页面都已过时或在某些方面不准确。

What's the scoop?

什么是独家新闻?

回答by Mark Harrison

Build ruby, gem, and rails

构建 ruby​​、gem 和 rails

as per http://rubyonrails.org/download:

根据http://rubyonrails.org/download

build ruby
build gem
use gem to install rails

Get Oracle Instantclient

获取 Oracle Instantclient

Download from https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html

https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html下载

You need these two packages for your architecture.

您的架构需要这两个包。

instantclient-basic
instantclient-sdk

Unzip these files, and make this link

解压缩这些文件,并制作此链接

cd instantclient_10_2
# .dylib for mac, .so for linux
ln -s libclntsh.dylib.10.1 libclntsh.dylib

Build ruby-oci8

构建 ruby​​-oci8

Note, JRuby users don't need ruby-oci8, but do need the Oracle JDBC jar, either ojdbc6.jar or ojdbc5.jar depending on whether you have Java 6 or Java 5.

请注意,JRuby 用户不需要 ruby​​-oci8,但需要Oracle JDBC jarojdbc6.jar 或 ojdbc5.jar,具体取决于您使用的是 Java 6 还是 Java 5。

Download from http://ruby-oci8.rubyforge.org/en/index.htmland run

http://ruby-oci8.rubyforge.org/en/index.html下载并运行

# DYLD for mac
export DYLD_LIBRARY_PATH=/path/to/instantclient_10_2
# LD for linux
export LD_LIBRARY_PATH=/path/to/instantclient_10_2
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install

Test with this line and your database connection string.

使用此行和您的数据库连接字符串进行测试。

ruby -r oci8 -e "OCI8.new('scott/tiger@orcl').exec('select * from user_tables') do |r| puts r.join(','); end"

Install activerecord-oracle_enhanced-adapter

安装 activerecord-oracle_enhanced-adapter

Note, notactivrecord-oracle-adapteras many older pages mention.

请注意,不是许多旧页面提到的activrecord-oracle-adapter

gem install activerecord-oracle_enhanced-adapter

Do that sweet rails thing

做那个甜蜜的铁轨事情

rails railstest
cd railstest
# edit config/database.yml as below
ruby script/generate scaffold comic title:string issue:integer publisher:string
rake db:migrate
ruby script/server

Test in browser

在浏览器中测试

<http://localhost:3000/comics>

config/database.yml

配置/数据库.yml

Use databaseif you have a TNS entry, otherwise use host. Note that you have three entries (devel, test, production) to update.

如果您有 TNS 条目,请使用database,否则使用host。请注意,您需要更新三个条目(开发、测试、生产)。

development:
    adapter: oracle_enhanced
    database: orcl           # format is tns-name entry
    host:  myorclhost/orcl   # format is hostname/instance-name
    username: scott
    password: tiger

References

参考

回答by Raimonds Simanovskis

Some additional links to previous answer.

上一个答案的一些附加链接。

If you are on a Mac then you can follow tutorial How to setup Ruby and Oracle Instant Client on Mac OS Xto get access to Oracle database from Ruby.

如果您使用的是 Mac,则可以按照教程如何在 Mac OS X 上设置 Ruby 和 Oracle Instant Client以从 Ruby 访问 Oracle 数据库。

Then you can read ActiveRecord Oracle enhanced adapter wikito get Oracle connectivity in Ruby on Rails. This adapter is used in many Ruby on Rails on Oracle projects and is under active maintenance.

然后,您可以阅读ActiveRecord Oracle 增强型适配器 wiki以获取 Ruby on Rails 中的 Oracle 连接。该适配器在许多 Ruby on Rails on Oracle 项目中使用,并且正在积极维护中。

I also regularly post about Ruby and Oracle at my blog.

我还定期在我的博客上发布有关 Ruby 和 Oracle 的信息。

回答by Boris Lopez

In my case I already had Ruby, gem, Rails and Oracle client on Windows 7. I installed ruby-oci8 binary package for Windows 32-bit:

就我而言,我已经在 Windows 7 上安装了 Ruby、gem、Rails 和 Oracle 客户端。我为 Windows 32 位安装了 ruby​​-oci8 二进制包:

Source: http://ruby-oci8.rubyforge.org/en/file.install-binary-package.html

来源:http: //ruby-oci8.rubyforge.org/en/file.install-binary-package.html

gem install ruby-oci8
gem install --platform x86-mingw32 ruby-oci8

And then:

进而:

gem install activerecord-oracle_enhanced-adapter

and you are ready to do Rails on Oracle.

并且您已准备好在 Oracle 上执行 Rails。

回答by tschlein

Just an update to reflect current versions (as of writing: August 2015):

只是反映当前版本的更新(截至撰写时:2015 年 8 月):

  • Install libaio(on Ubuntu/Debian Linux probably with apt-get install libaio-dev- 0.3.109-4)
  • Download Oracle Instant Client (12.1.0.2.0, Basic + SDK should be enough, have not yet tried Basic Lite instead of Basic though)
  • Set environment variables (fit to your needs):

    export LD_LIBRARY_PATH=/path/to/the/Instant/Client/directory
    export NLS_LANG="German_Germany.WE8ISO8859P1"
    
  • Ruby (2.2.1p85), gem (2.4.6), Rails (4.2.3), ruby-oci8 (2.1.8, https://github.com/kubo/ruby-oci8) and activerecord-oracle_enhanced-adapter (1.6.2, https://github.com/rsim/oracle-enhanced) can be installed the usual way (I used rvm(1.26.11) for this - https://rvm.io/), so basically this should do the job for the three gems:

    gem install rails ruby-oci8 activerecord-oracle_enhanced-adapter
    
  • Create your Rails app

  • Configure your Rails app:

    • in Gemfileadd activerecord-oracle_enhanced-adapterand ruby-oci8:

      gem 'activerecord-oracle_enhanced-adapter'
      gem 'ruby-oci8'
      
    • in config/database.ymladd your Oracle environment:

      development:
        adapter: oracle_enhanced
        host: my-db-host
        port: my-port
        database: my-db
        username: my-user
        password: my-password
      
    • Let Bundle grab the gems: bundle install:

      ...
      Using activerecord-oracle_enhanced-adapter 1.6.2
      ...
      Using ruby-oci8 2.1.8
      ...
      
    • Ready to rumble:

      rails generate scaffold book title:string author:string
      rake db:create db:migrate
      ...
      
  • 安装libaio(在 Ubuntu/Debian Linux 上可能使用apt-get install libaio-dev- 0.3.109-4)
  • 下载 Oracle Instant Client(12.1.0.2.0,Basic + SDK 应该就够了,虽然还没有试过 Basic Lite 而不是 Basic)
  • 设置环境变量(适合您的需要):

    export LD_LIBRARY_PATH=/path/to/the/Instant/Client/directory
    export NLS_LANG="German_Germany.WE8ISO8859P1"
    
  • Ruby (2.2.1p85)、gem (2.4.6)、Rails (4.2.3)、ruby-oci8 (2.1.8, https://github.com/kubo/ruby-oci8) 和 activerecord-oracle_enhanced-adapter ( 1.6.2, https://github.com/rsim/oracle-enhanced) 可以按照通常的方式安装(我使用rvm(1.26.11) 为此 - https://rvm.io/),所以基本上应该这样做三颗宝石的工作:

    gem install rails ruby-oci8 activerecord-oracle_enhanced-adapter
    
  • 创建您的 Rails 应用

  • 配置您的 Rails 应用程序:

    • Gemfile添加activerecord-oracle_enhanced-adapterruby-oci8

      gem 'activerecord-oracle_enhanced-adapter'
      gem 'ruby-oci8'
      
    • config/database.yml加入Oracle环境:

      development:
        adapter: oracle_enhanced
        host: my-db-host
        port: my-port
        database: my-db
        username: my-user
        password: my-password
      
    • 让 Bundle 抢夺珍宝bundle install

      ...
      Using activerecord-oracle_enhanced-adapter 1.6.2
      ...
      Using ruby-oci8 2.1.8
      ...
      
    • 准备隆隆声:

      rails generate scaffold book title:string author:string
      rake db:create db:migrate
      ...
      

Note:

笔记:

Depending on your Oracle environment (i.e. my-useralready exists or has to be created), you can omit the db:createstatement or not. In the latter case, rakeprompts you for the SYS/SYSTEM password in order to be able to create the user and grant it the corresponding privileges.

根据您的 Oracle 环境(即my-user已经存在或必须创建),您可以省略db:create或不省略该语句。在后一种情况下,rake会提示您输入 SYS/SYSTEM 密码,以便能够创建用户并授予其相应的权限。