0

I'm modifying a Ruby Gem to allow it to modify a Rails database via a rake task.

I was able to connect the database and modify its content by passing the database configuration as parameters to the rake task, which then called the ActiveRecord::Base.establish_connection. My question is, how could the Gem connect to the Rails database without having to specify the database configuration as parameters in the rake task.

1 Answer 1

2

Assuming the gem has access to the Rails.application, you can access the database config file with

db_config = Rails.application.config.database_configuration[Rails.env]

If it doesn't, you can parse the file yourself.

db_config = YAML.load(ERB.new(File.read('./config/database.yml')).result)

Once you have your configuration, you can pass it into the connector.

Sign up to request clarification or add additional context in comments.

1 Comment

My gem has access to Rails.application and the first line works well, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.