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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1643875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:53:35  来源:igfitidea点击:

How to use ActiveRecord in a ruby script outside Rails?

ruby-on-railsrubyactiverecordscripting

提问by Daniel Cukier

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?

我有一个小的 ruby​​ 脚本,我想在其中使用 ActiveRecord 轻松访问数据库模型。最好的方法是什么?

回答by Pesto

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

回答by Andy Henson

It's worth noting that in later versions of activerecord (v3+) you need to require it like so

值得注意的是,在 activerecord (v3+) 的更高版本中,您需要像这样要求它

require "active_record"

回答by Thomas Klemm

You can create a minimal script with an in-memory SQLite database in just a few lines. This answer is also available as a Gist.

只需几行,您就可以使用内存中的 SQLite 数据库创建一个最小的脚本。此答案也可作为 Gist 使用

Inspired by Jon Leighton's blog poston how to post an awesome ActiveRecord bug report.

灵感来自Jon Leighton 的关于如何发布令人敬畏的 ActiveRecord 错误报告的博客文章



# Based on http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/ 

# Run this script with `$ ruby my_script.rb`
require 'sqlite3'
require 'active_record'

# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'

# Connect to an in-memory sqlite3 database
ActiveRecord::Base.establish_connection(
  adapter: 'sqlite3',
  database: ':memory:'
)

# Define a minimal database schema
ActiveRecord::Schema.define do
  create_table :shows, force: true do |t|
    t.string :name
  end

  create_table :episodes, force: true do |t|
    t.string :name
    t.belongs_to :show, index: true
  end
end

# Define the models
class Show < ActiveRecord::Base
  has_many :episodes, inverse_of: :show
end

class Episode < ActiveRecord::Base
  belongs_to :show, inverse_of: :episodes, required: true
end

# Create a few records...
show = Show.create!(name: 'Big Bang Theory')

first_episode = show.episodes.create!(name: 'Pilot')
second_episode = show.episodes.create!(name: 'The Big Bran Hypothesis')

episode_names = show.episodes.pluck(:name)

puts "#{show.name} has #{show.episodes.size} episodes named #{episode_names.join(', ')}."
# => Big Bang Theory has 2 episodes named Pilot, The Big Bran Hypothesis.

# Use `binding.pry` here to experiment with this setup.