简体   繁体   中英

how to require active record working outside of rails

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?

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
require 'rubygems'
require 'active_record'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM