Rahul Jain
Rahul Jain

Reputation: 1

Adding a gem dynamically from within a rake task

I am trying to use the mongo gem for a data migration rake task. I don't want to add it to the Gemfile for the whole project just so it can be used from this single rake task. How do I dynamically add mongo to the bundle for just that rake task?

I have tried using Bundler::Injector::inject, but then I need to bundle install. If I run that from within the task, the bundler has already initialized, so the require 'mongo' still fails. Should I do something to reload the bundler or is there actually a clean way to do this?

Upvotes: 0

Views: 106

Answers (1)

showaltb
showaltb

Reputation: 998

Add it to your Gemfile with :require => false:

gem "mongo", :require => false

This will allow bundler to install it and set up the load path, but it won't actually load the gem.

In your rake task, just require "mongo" to load it when you need it.

Upvotes: 1

Related Questions