如何要求在 Rails 之外工作的活动记录 [英] how to require active record working outside of rails

查看:18
本文介绍了如何要求在 Rails 之外工作的活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要活动记录,但我在 Rails 之外工作(原因如下:简单的 Ruby 输入验证库).我需要整个 rails gem,还是可以做 DRYer?

i need to require active record, but I am working outside of rails (here is why: Simple Ruby Input Validation Library). do I need to require the entire rails gem, or can i be DRYer?

推荐答案

下面是我在 Rails 之外使用 ActiveRecord 的方法:

Here's how I'm using ActiveRecord outside of Rails:

#!/usr/bin/ruby

require 'active_record'
require 'mysql2' # or 'pg' or 'sqlite3'

ActiveRecord::Base.establish_connection(
  adapter:  'mysql2', # or 'postgresql' or 'sqlite3'
  database: 'DB_NAME',
  username: 'DB_USER',
  password: 'DB_PASS',
  host:     'localhost'
)

# Note that the corresponding table is 'orders'
class Order < ActiveRecord::Base
end

Order.all.each do |o|
  puts "o: #{o.inspect}"
end

这篇关于如何要求在 Rails 之外工作的活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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