【问题标题】:How to use ActiveRecord in a ruby script outside Rails?如何在 Rails 之外的 ruby​​ 脚本中使用 ActiveRecord?
【发布时间】:2010-12-11 06:16:10
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby activerecord scripting


    【解决方案1】:
    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
    

    【讨论】:

    • 未定义方法 `require_gem' - 你的意思是 gem "activerecord"?
    • 不推荐使用 require_gem 调用。现在应该需要“activerecord”。
    • 另一个相关问题:如何拥有一个环境敏感的database.yml?
    • 它是 'gem install activerecord' 并且需要 'active_record',我知道,令人困惑
    【解决方案2】:

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

    require "active_record"
    

    【讨论】:

    • 感谢您的回答。但是“active_record”实际上是从哪里加载的呢?
    【解决方案3】:

    只需几行代码,您就可以使用内存中的 SQLite 数据库创建一个最小的脚本。这个答案也是available as a Gist

    Jon Leighton's blog post 启发,了解如何发布出色的 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.
    

    【讨论】:

    • 这很棒,作为一个开始。那么我该如何从这个转移到一个持久的数据库呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2013-10-12
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多