Search code examples
ruby-on-railsruby-on-rails-4ruby-on-rails-5bundler

Getting rails console to work with multiple Gemfiles


We are currently migrating from Rails 4 to 5, and have two Gemfiles (similar to how GitHub did it), Gemfile (Rails 4) and Gemfile_5 (Rails 5).

The following commands work as expected:

bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.11.12 LTS application starting in development on http://localhost:3000

BUNDLE_GEMFILE=Gemfile_5 bundle exec rails s
=> Booting WEBrick
=> Rails 5.0.7.1 application starting in development on http://localhost:8000
bundle exec rspec spec/...
# runs specs using Rails 4 gemset

BUNDLE_GEMFILE=Gemfile_5 bundle exec rspec spec/...
# runs specs using Rails 5 gemset
bundle exec rails --version
Rails 4.2.11

BUNDLE_GEMFILE=Gemfile_5 bundle exec rails --version
Rails 5.0.7.1

And yet, when trying to run console or runner, it'll only use the Rails 4 gemset:

BUNDLE_GEMFILE=Gemfile_5 bundle exec rails c
Loading development environment (Rails 4.2.11.12 LTS)
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails r "puts Rails.version"
4.2.11

I've tried restarting Spring, but that hasn't had any effect. What am I missing here?


Solution

  • The trick was to fully disable Spring, rather than just restart it:

    DISABLE_SPRING=1 BUNDLE_GEMFILE=Gemfile_5 bundle exec rails console
    Loading development environment (Rails 5.0.7.1)