[英]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 ). 我需要活动记录,但我在rails之外工作(这就是为什么: 简单的Ruby输入验证库 )。 do I need to require the entire rails gem, or can i be DRYer?
我需要整个rails gem,还是我可以DRYer?
Here's how I'm using ActiveRecord outside of Rails: 以下是我在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
require 'rubygems'
require 'active_record'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.