Skip to content

Instantly share code, notes, and snippets.

@pat
Created July 25, 2018 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pat/fcd2173742ec490a7dc3f75676b652e5 to your computer and use it in GitHub Desktop.
Save pat/fcd2173742ec490a7dc3f75676b652e5 to your computer and use it in GitHub Desktop.
Using ActiveRecord in a Rake-based application
# config/application.rb
RAKE_PATH = File.expand_path(".")
RAKE_ENV = ENV.fetch("RAKE_ENV", "development")
Bundler.require :default, RAKE_ENV
# Configuration for ActiveRecord rake tasks
ActiveRecord::Tasks::DatabaseTasks.root = RAKE_PATH
ActiveRecord::Tasks::DatabaseTasks.env = RAKE_ENV
ActiveRecord::Tasks::DatabaseTasks.db_dir = "db"
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = ["db/migrate"]
ActiveRecord::Tasks::DatabaseTasks.seed_loader = nil
# Set up ActiveRecord database connection
ActiveRecord::Base.configurations = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection RAKE_ENV.to_sym
# Add local lib directory to the load path
$LOAD_PATH.unshift File.join(RAKE_PATH, "lib")
# config/database.yml
development:
adapter: postgresql
database: my_app_development
test:
adapter: postgresql
database: my_app_test
require "bundler/setup"
load "active_record/railties/databases.rake"
task :environment do
require "./config/application"
require "my_app"
end
task :console => :environment do
require "irb"
ARGV.clear
IRB.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment