Use older version of Rake - ruby-on-rails

I have Rake version 0.9.1 but I need to use 0.8.7 for a project, and I'm fairly certain I have both version installed but it always uses 0.9.1 by default. Is there a way to specify which version of Rake to use?
I'm trying to run this: rake db:drop db:create db:migrate db:seed
and I get this error:
You have already activated rake 0.9.1, but your Gemfile requires rake 0.8.7. Consider using bundle exec.

gem search (or list) rake, should tell you which versions are installed.
You can invoke rake with a specific version number bracketed with
underscores.
$rake _0.7.3_
This is a standard feature of gem packaged binaries.

You can specify the version of Rake to use, in your Gemfile:
gem 'rake', '0.8.7'
Though the "error" message you are getting says it all... you need to run:
bundle exec rake ...
... in order to use the right rake to run your rake tasks.
More info on bundle exec: http://gembundler.com/man/bundle-exec.1.html

Try executing gem uninstall rake then just pick the version you want to uninstall.

It happens because you are using rake from the system. (latest version by default)
The solution is use follow command:
bundle exec rake db:migrate
Also, you can create alias. Because this command is too big and difficult to write.
echo "alias be='bundle exec'" >> ~/.bash_profile
source ~/.bash_profile
Then you can use follow short command:
be rake db:migrate

Related

Rails migration error when running migration?

When I do rake db:migrate, I get the following error:
rake aborted!
Gem::LoadError: You have already activated rake 10.2.2, but your Gemfile requires rake 10.1.0. Using bundle exec may solve this.
How can solve this?
This error is due to the fact that some applications may specify different versions of gems than the ones you have installed.
Try using bundle exec rake db:migrate.
Using bundle exec guarantees that the program is run with the environment specified in the gemfile.
Perhaps:
bundle exec rake db:migrate
There might be other gems in the Gemfile which are dependent on rake 10.2.2, while you are trying to install rake 10.1.0 via your gemfile or explicitly mentioned it. A look into your Gemfile will help.
In case you have specific environment, you may want to run
bundle exec rake db:migrate
to make sure you are running it appropriately.
As per another answer given on this topic, you can also try deleting the Gemfile.lock file and re-running bundle install to regenerate the Gem dependencies.

Use bundle exec rake or just rake?

I learned Rails using just the rake command like rake db:migrate; however, I read that I should be using the bundle exec rake ... instead of just plain rake. Now I'm confused about which to use.
Should I be using bundle exec rake instead of just plain rake or is it just a preference thing? Any insight would be much appreciated! Thanks!
bundle exec executes a command in the context of your bundle.
That means it uses the gems specified in your Gemfile. Much of the time, running bundle exec rake foo has the same results as if you just ran rake foo, especially if you have the same gems installed systemwide as in your Gemfile. However, some applications may specify different versions of gems than the ones you have installed systemwide, and may want those exact gems and versions to be able to run correctly. If you just run without bundle exec, you may get some weird errors.
Using bundle exec guarantees that the program is run with the environment specified in the gemfile, which hopefully means it is the environment that the creators of the program want it to be run in, which hopefully means it should run correctly no matter what weird setup you have on your computer.
It basically standardizes the environment under which the program is run. This helps avoid version hell and makes life much easier.
See http://bundler.io/v1.3/man/bundle-exec.1.html for more info.
$ bundle exec rake db:migrate
Uses the version of rake specified in the Gemfile to execute the rake task db:migrate.
But there is no rake gem specified in the Gemfile!
Yes, but a rake gem was installed as a dependency of some other gem--look in Gemfile.lock. So the rule must be: Uses the version of rake specified in Gemfile.lock.
But Gemfile.lock doesn't specify a specific version--it specifies a version greater than x.y!
Then the rule must be: Uses the version of rake that was installed in the current gemset.
$ rake db:migrate
Normally, when you issue a command on the command line, e.g. rake, your system searches for the command in the list of directories specified in your PATH environment variable. The first directory that contains the command is the version of the command that is used. To see which directory that is, you can do:
$ which rake
So if you execute,
$ rake db:migrate
that may use a different rake gem than the one you installed with bundle install. But, even if your system finds the same rake version as bundle exec, any gems required by the rake source code will be searched for in places outside your project's gemset. Therefore, there are many ways that just:
$ rake db:migrate
can screw things up.
According to the Ruby on Rails Tutorial Book(free online), section 3.6, if you are using rvm 1.11.x+ then you do not need to preface commands with bundle exec.
running any exacutable without bundle exec will have Rubygems fetching the latest version of the gem installed in your system.
By adding the bundle exec prefix instead will have the executable running in the context of your Gemfile.lock, which means that will be run using the version defined in the gem file.

newbie: error message when 'rake -T'

I am using Ruby Enterprise Edition for my project. When I check all my rake task by run the command rake -T , I got the following error message:
You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Using bundle exec may solve this.
The error message implies that I can use bundle exec to solve the problem, but I am not sure how? So, how to get rid of this error message?
------------------------------ more ---------------------------
I prefer to update my Gemfile instead of run bundle exec rake -T. But when I open my project Gemfile, I did not see rake 0.9.2 in my Gemfile, why the error message complains that I have it? Where could be the place I defined rake 0.9.2??
Run bundle exec rake -T, this ensures that the version of rake that is specified in your Gemfile is running, not another version.
Alternatively, update your Gemfile.
This is because your rake tool does not match the version written in the Gemfile.
You first need to run this command, to ensure rake 0.9.2 get installed:
bundle install
Then, you can run rake 0.9.2 with the following command:
bundle exec rake -T
The bundle thing is a nice tool to help you manage the dependency of your application. You can get more info from here.

Problems with Rake and Ruby on Rails

I have been trying to run rake but it seems that ever since I updated ruby gems rake is failing.
This morning I ran:
gem update --system
And ever since, rake has been failing with the following error:
$ rake db:migrate
rake aborted!
undefined method `specifications' for "/usr/lib/ruby/gems/1.9.1":String
/home/cknadler/projects/ecommerce/Rakefile:7:in `<top (required)>'
(See full trace by running task with --trace)
I have been reading about this problem and it seems that there is a problem with rake 0.9.x that breaks rails but when I check my rake version, I am running 0.8.7:
$ rake --version
rake, version 0.8.7
I have tried uninstalling rake and reinstalling it, using bundler, etc and at this point I am pretty stuck. Thanks in advance.
Edit:
My Rakefile (located in my app root directory)
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
Ecommerce::Application.load_tasks
I would suggest using the bundled binary version of rake to avoid this issue.
bundle exec rake db:migrate
If you installed your bundle using binstubs (bundle install --binstubs) then you can also use the bin version of rake which is equivalent to the bundle exec rake command:
bin/rake db:migrate
P.S: I would also recommend using RVM instead of installing Ruby using sudo for all users. This allows you to keep more modular ruby and gem installation.
You should remove rake 0.9.x (you may have 0.9.2 installed) by doing
gem uninstall rake -v=0.9.2
And then run bundle update
bundle update
Hope that helps.

Rake gems:install says "could not find table", rake db:migrate says "required gems missing"

I just cloned a github repo on a fresh Ubuntu machine, running sqlite3 for all environments. rake db:create says development.sqlite3 already exists. rake db:migrate says I'm missing a bunch of required gems and should run rake gems:install. rake gems:install, of course, says it Could not find table 'studies', which sounds to me like something rake db:migrate should fix.
I looked around the net and while lots of people have gotten 'could not find table' errors, they all got them from rake db:migrate, not rake gems:install. I'm suspecting it's an application-specific error, but still, any ideas would be appreciated.
PS: Ruby 1.8.7, Rails 2.3.8.
You can always install separate gems with gem install -r <gem name> or gem install -v=<gem version> -r <gem name> command, not using rake.
Is this a Rails 3 app? If so, you should run:
bundle install
Rails 3 uses Bundler instead of the rake tasks to manage gems. http://gembundler.com/
Did you try running with the trace option? Might help pin down the failing gem:
rake -t gems:install

Resources