现在的位置: 首页 > 综合 > 正文

How to use ActiveRecord in a ruby script outside Rails?

2018年04月18日 ⁄ 综合 ⁄ 共 490字 ⁄ 字号 评论关闭
require "rubygems"
require "activerecord"
# Use the following line instead (without the comment hash) if you are using JRuby
#require "active_record"

#Change this to reflect your database settings
ActiveRecord::Base.establish_connection (
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :password => "password",
  :database => "some_database")

#Now define your classes from the database as always
class SomeClass < ActiveRecord::Base
  #blah, blah, blah
end

#Now do stuff with it
some_class = SomeClass.new
some_other_stuff = SomeClass.find :all

抱歉!评论已关闭.