how to require active record working outside of rails

做~自己de王妃 提交于 2019-11-27 04:20:47

问题


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?


回答1:


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



回答2:


require 'rubygems'
require 'active_record'


来源:https://stackoverflow.com/questions/3661546/how-to-require-active-record-working-outside-of-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!