Rails rails 项目需要读取 oracle 中的某些表数据,求方法

zhbinx · 2013年12月04日 · 最后由 zhbinx 回复于 2013年12月10日 · 2878 次阅读

RT

其实我想问两个内容

  • 能不能在 rails 项目启动时就连接两个数据库,一个是存储自己数据的 mysql,另外一个是其他系统的 oracle 数据库
  • 假如是一般的 Ruby 程序连接 oracle 的话,该用哪个 gem 呢,或者谁直接贴我点代码也行

谢谢

在 database.yml 里添加另一组配置 记好了名字 然后 model 这样写

class OtherRecord < ActiveRecord::Base
    establish_connection :other
end

#1 楼 @zj0713001 cooool,谢谢,我去试试

#1 楼 @zj0713001 请问我这样连一个 oracle,连接成功了,defect.all也有内容了 但是Defect.where(:defect_id=>123).bg_summary却显示bg_summaryundefined method 请问我漏了什么? 谢谢

qc:
  adapter: oracle_enhanced
  host: 123.123.123.123
  encoding: utf8
  database: db
  username: system
  password: system
  port: 1521
class Defect < ActiveRecord::Base
  establish_connection :qc
  self.table_name='bug'
end
gem 'activerecord-oracle_enhanced-adapter', '~> 1.4.0'
gem 'ruby-oci8', '~> 2.1.0'

bg_summary 是什么东西...

#3 楼 @zhbinx 如果是表里的字段 因为你 where 出来的不是一个对象 而是一个对象列表 所以假如你的 defect_id 可以确定单个记录的话 推荐用 Defect.find_by_defect_id(123).bg_summary 这样能直接调用

#5 楼 @zj0713001 哎呀我去。。。我都忘记加.each了。。。。好 2.。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号