Created
July 25, 2018 03:29
-
-
Save pat/fcd2173742ec490a7dc3f75676b652e5 to your computer and use it in GitHub Desktop.
Using ActiveRecord in a Rake-based application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/database.yml | |
development: | |
adapter: postgresql | |
database: my_app_development | |
test: | |
adapter: postgresql | |
database: my_app_test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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