Reputation: 5178
The situation is that my default gemset on my computer points to rails 5.0.0
. However: I have an app that uses a really old rails version. Lets say for this example the app uses: rails 2.1.1
.
Does my machine automatically switch to rails 2.1.1
when cd'ed
into this app because rails 2.1.1
is specified in the Gemfile
? Or: do I need to explicitly create a gemset in order for my app and my machine to do things the "rails 2.1.1 way" (as opposed to doing stuff the "rails 5.0.0 way which is the rails version in my default gemset)?
My concern is that perhaps if my machine thinks I am using rails 5.0.0
instead of rails 2.1.1
while developing inside the app: then rails commands such as generators might create files and do stuff "the rails 5.0.0 way" as opposed to the "rails 2.1.1" way.
Hopefully this makes sense. Here is what I would do in order to "explicitly" state via a Gemset that this app uses rails 2.1.1
Example:
I create a gemset that is to be specified for any app that uses rails 2.1.1
rvm gemset create rails_2_1_1
I specify the ruby version to use on this gemset
rvm use 2.2.1@rails_2_1_1
I then install that old version of rails onto this gemset:
gem install rails --version=2.1.1
Now this gemset uses rails 2.1.1
.
Now at the root of my app I specify a .ruby-gemset
file that tells rails: "Make sure you are doing stuff the rails 2.1.1
way and not the rails 5.0.0
way:
#.ruby-gemset
rails_2_1_1
I want to be sure that if another rails version is specified in an app's Gemfile than is in my default gemset: then developing within that app will do everything within the context of the rails version specified in the Gemfile as opposed to the rails version in the default gemset.
Upvotes: 3
Views: 5317
Reputation: 2084
That's not a problem with RVM and Rails. Using the Gemfile is the best way to do this, IMHO! In your Gemfile, you can specify which ruby and which gemset within that ruby to use.
First, lets establish that default ruby for the system on 5.0.0
. This will allow any new/existing Rails projects to default to this ruby version (except for those projects that override with the Gemfile):
rvm use --default 5.0.0
..and of course, if you want it fixed to a specified gemset:
rvm use --default 5.0.0@my_default_gemset
If you want to setup your rails app to utilize the RVM gemset 2.2.1@rails_2_1_1
, similar to the RVM command below...
rvm use 2.2.1@rails_2_1_1
In your Gemfile, specify right below the source
line the following two commented lines:
source 'https://rubygems.org'
#ruby=2.2.1
#ruby-gemset=rails_2_1_1
Now, when you cd
into your rails' app directory, you should receive the following message, or similiar:
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does
that too, you can ignore these warnings with 'rvm rvmrc warning ignore
/my/rails/app/path/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore
allGemfiles'.
You can double check your results:
rvm list gemsets
ruby-2.2.1 [ x86_64 ]
ruby-2.2.1@global [ x86_64 ]
=> ruby-2.2.1@rails_2_1_1 [ x86_64 ]
ruby-5.0.0 [ x86_64 ]
ruby-5.0.0@global [ x86_64 ]
Another example using [email protected]
, example RVM command...
rvm use [email protected]
In your Gemfile, specify:
#ruby=2.0.0-p247
#ruby-gemset=rails-4.0.0
Upvotes: 3
Reputation: 868
First of all, I recommend against using .ruby-gemset
file, because Gemfile
already has everything you'll need. .ruby-version
is obsolete too, because Ruby version can be specified inside Gemfile
as well:
source 'https://rubygems.org'
ruby '2.3.1'
gem 'rails', '~> 4.2.7'
...
Now about Rails versions. As long as you run commands with bundle exec
, these commands' versions will be exactly as stated in Gemfile
.
There are also binstubs
scripts inside projects bin/
folder. They're the most preferable way to run commands, because they're usually tweaked for project needs (for example running spring
along with your command) and they ultimately run bundle exec
anyways.
So the proper way to do things, is to run ./bin/rails
, which can be tedious. That's why I recommend using helpers. For example, oh-my-zsh project has out-of-the-box rails
plugin. First few lines (read below for explanation):
function _rails_command () {
if [ -e "bin/rails" ]; then
bin/rails $@
elif [ -e "script/rails" ]; then
ruby script/rails $@
elif [ -e "script/server" ]; then
ruby script/$@
else
command rails $@
fi
}
function _rake_command () {
if [ -e "bin/rake" ]; then
bin/rake $@
elif type bundle &> /dev/null && [ -e "Gemfile" ]; then
bundle exec rake $@
else
command rake $@
fi
}
alias rails='_rails_command'
compdef _rails_command=rails
alias rake='_rake_command'
compdef _rake_command=rake
When you run rails
, this script checks if bin/rails
exists (Rails 5 and 4 default) and runs it instead. If not, script checks for script/rails
(Rails 3 default) and script/server
(Rails 2 default). So basically you have all versions covered. Same is for rake
.
With this plugin you never need to worry about extra commands. Always simply run rails
. You just install it once and forget about it.
Upvotes: 0
Reputation: 354
You can also create rvmrc file into your project directory then it will switch rvm automatically
Please follow the process to create rvmrc file.
Go to project directory
rvm use 2.2.1@rails_2_1_1
rvm --rvmrc --create 2.2.1@rails_2_1_1
I think it would help you.
Upvotes: 1
Reputation: 71
You can create a .ruby-version
file in your project's folder and RVM will automatically switch to the correct Ruby. Something like this:
$ echo '2.1.1' > .ruby-version
You just need to make sure you have that version installed, eg.:
$ rvm install ruby-2.1.1
Upvotes: 0