在rake任务中使用database.yml而不是提供user / pass

时间:2014-05-22 19:47:21

标签: ruby-on-rails ruby

我如何转换(这有效):

desc "Import Bar"
task :import_bar => :environment do
  OCI8.new('user', 'pass', 'database').exec('select foo from bar') do |r| 
    puts r
  end
end

这样的事情(不工作):

desc "Import Bar"
task :import_bar => :environment do
  ActiveRecord::Base.connection.execute('select foo from bar') do |r|
    puts r
  end
end

的database.yml

development:
  adapter: oracle_enhanced
  database: database
  username: user
  password: pass
  pool: 5
  timeout: 5000

rake import_bar

无输出

1 个答案:

答案 0 :(得分:1)

您忘记使用fetch迭代对象,请尝试:

desc "Import Bar"
task :import_bar => :environment do
  ActiveRecord::Base.connection.execute('select foo from bar').fetch() do |r|
    r.join(',')
  end
end