如何要求在轨道外工作的活动记录 [英] how to require active record working outside of 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
这篇关于如何要求在轨道外工作的活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文