Ruby PostgreSQL教程 [英] Ruby PostgreSQL tutorials

查看:239
本文介绍了Ruby PostgreSQL教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个与PostgreSQL数据库交互的ruby脚本。我正在尝试从文档中拼凑出如何执行此操作的方法,但是一个不错的教程或示例代码会有效地减少所需的时间。如果有人有链接,一些提示或他们可以分享的一些代码,我将不胜感激。

I am trying to write a ruby script that interacts with a PostgreSQL database. I am trying to piece together how to do this from the documentation, but a nice tutorial or sample code would work wonders to decrease the amount of time to get this working. If anyone has a link, some tips or has some code they could share I would be most grateful.

编辑,使此注释更清晰:

Edit, made this note more clear:

注意:这与Rails ActiveRecord无关,我正在编写一个Ruby脚本,该脚本将包含在与Rails完全独立的程序中。

Note: this isn't to do with rails ActiveRecord, I am writing a Ruby script that will be involved in a program that is completely independent from Rails.

推荐答案

您只需要 pg gem并建立连接到数据库:

You only need to require the pg gem and establish the connection to the DB:

require 'pg'
# require 'active_record'  # uncomment for not Rails environment

ActiveRecord::Base.establish_connection(:adapter => "postgresql",
                                        :username => "username",
                                        :password => "password",
                                        :database => "database")

当您定义要从<$ c $继承的模型时c> ActiveRecord :: Base ,他们将使用此数据库连接。其他一切都应该像在Rails中一样工作。

When you define models to inherit from ActiveRecord::Base they will use this database connection. Everything else should work like it does in Rails.

这篇关于Ruby PostgreSQL教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆