Getting rails console to work with multiple Gemfiles - ruby-on-rails

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?

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)

Related

Cannot start rails console unless spring is disabled

My rails development environment runs in Docker, and I cannot start the rails console without disabling spring using DISABLE_SPRING=true. The snipper below illustrates what happens:
app#docker:[project] $ bundle exec rails c
Could not find rake-13.0.6 in any of the sources
Run `bundle install` to install missing gems.
app#docker:[project] $ DISABLE_SPRING=true bundle exec rails c
D, [2022-01-19T03:44:17.663136 #20] DEBUG -- sentry: initialized a background worker with 6 threads
Loading development environment (Rails 6.1.4.4)
irb(main):001:0>
I've tried rebuilding my docker image from scratch, deleting the Gemfile.lock and building again. Nothing works, except for disabling spring.
Surprisingly, rails server works fine.
Any ideas?

rails c wont work in app directory

Im trying to reach the production env console. I think rbenv is messing with me.
When i run rails console production i get the old:
Usage:
rails new APP_PATH [options]
So i figure, it doesn't recognize my dir as an rails app because of the versions so I run. So the rails -v tells me:
deploy#webb-labb2:~/prognoser/current$ rails -v
Rails 5.1.0
Which is the wrong version of the app.
And the bundle exec rails -v command gives me:
deploy#webb-labb2:~/prognoser/current$ bundle exec rails -v
Rails 4.2.8
Which is the correct version. But the:
deploy#webb-labb2:~/prognoser/current$ bundle exec rails console production
Still gives me the old:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH]
I ran
$ bundle exec rake rails:update:bin
that did the trick. And I had to add a boot file to the shared/config directory.

Unable to run `rails g` or `rails c` without `sudo`

I am unable to run rails g or rails c, because of the following error message:
Could not find mysql2-0.3.18 in any of the sources
Run `bundle install` to install missing gems.
I can run 'rails server', which works fine, as expected. I also have ran bundle install, with no errors (this line is in my Gemfile: gem 'mysql2'). The database works fine with rails server, but seems to break with rails g/c.
Update:
I can run rails g and rails c with sudo. I think it's a problem with my path - I think I might have installed MySQL as root.
Have you tried running those commands using Bundler?
bundle exec rails c
bundle exec rails g

How to run a custom command when run the 'rails server' command?

I am using Ruby on Rails 3.1.0 and the DelayedJob gem. In order to automate processes in development mode I would like to "auto"-run the following command
rake jobs:work
when in my console\terminal I run this other command
rails server
# and/or
#
# rails db:create
# rails db:migrate
# ...
Is it possible? How can I do that?
The gem "foreman" will sort this out for you. Here's the railscast: http://railscasts.com/episodes/281-foreman

How can I start the rails console and use the testing database exclusively?

I'd like to start the rails console and create database entries in a database that isn't the default database, such as the testing database. I'd appreciate any help.
To start console in test environment:
ruby script/console test
ruby script/console production
To run rake tasks in test environment:
rake db:create RAILS_ENV=test
rake db:migrate RAILS_ENV=test
In Rails 3 and 4 you can use:
rails console test
rails c test
In Rails 5 you can use:
rails console -e test
rails c -e test
You can pass the environment as RAILS_ENV=test:
$ RAILS_ENV=test bundle exec rails console
Or:
$ RAILS_ENV=test bundle exec rails c
You can also do:
$ bundle exec rails console test
Or:
$ bundle exec rails c test
You can see like
Loading test environment (Rails 3.2.8)
1.9.3p327 :001 >
in Rails 6 the syntax has changed for this
rails c -e test
$ RAILS_ENV=test ./script/console

Resources