如何要求在rails之外工作的活动记录

时间:2010-09-07 18:32:35

标签: ruby-on-rails ruby activerecord rubygems require

我需要激活记录,但我在rails之外工作(这就是原因:Simple Ruby Input Validation Library)。我需要整个rails gem,还是我可以DRYer?

2 个答案:

答案 0 :(得分:47)

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

#!/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

答案 1 :(得分:7)

require 'rubygems'
require 'active_record'