如何在 Rails 之外的 ruby 脚本中使用 ActiveRecord? [英] How to use ActiveRecord in a ruby script outside Rails?
本文介绍了如何在 Rails 之外的 ruby 脚本中使用 ActiveRecord?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小的 ruby 脚本,我想在其中使用 ActiveRecord 轻松访问数据库模型.最好的方法是什么?
I have a small ruby script in which I'd like to use ActiveRecord to easily access a database model. What is the best way to do it?
推荐答案
require 'active_record'
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'mysql2', # or 'postgresql' or 'sqlite3' or 'oracle_enhanced'
host: 'localhost',
database: 'your_database',
username: 'your_username',
password: 'your_password'
)
# Define your classes based on the database, as always
class SomeClass < ActiveRecord::Base
#blah, blah, blah
end
# Now do stuff with it
puts SomeClass.find :all
some_class = SomeClass.new
这篇关于如何在 Rails 之外的 ruby 脚本中使用 ActiveRecord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文