如何在 Rails 之外的 ruby​​ 脚本中使用 ActiveRecord?

2024-03-29

我有一个小的 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rails 之外的 ruby​​ 脚本中使用 ActiveRecord? 的相关文章

随机推荐