我有一个小的 ruby 脚本,我想在其中使用 ActiveRecord 轻松访问数据库模型。最好的方法是什么?
最佳答案
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
关于ruby-on-rails - 如何在 Rails 之外的 ruby 脚本中使用 ActiveRecord?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1643875/