Multiple Rails versions for multiple projects - ruby-on-rails

I'd like to use Rails 2.2.2 for one project and Rails 2.3.2 for another. Both are installed.
What is rails _2.2.2_ --version supposed to do? I've read that it makes 2.2.2 the working version -- that is, the version that will be used from that point on. But when I check rails --version, I get Rails 2.3.2. So, I also want to know what rails --version tells me; is it just the latest version of Rails that I have or is it the version that will be used for rakes?
I know about RVM. Is that the best way to use different versions of Rails on different projects?

Yes "rails --version" tells which is the latest version of rails. If you want to check different versions of rails installed in your machine you have to do "gem list" which gives the list of all the gems installed in your machine with their versions.For Ex:-
rack (1.0.0)
rails (2.3.4, 2.1.1)
rake (0.8.7)
Is that the best way to use different versions of Rails on different projects?
My point of view is that No it's not a good practise to use diiferent Rails version for the different projects. but sometime you used your old projects which is of an earlier version so you have no option but install that rails version. But while creating a new application you should used the latest stable rails version.
Just for record you can used different Rails version for different projects by mentioning Rails version in enviorment.rb For Ex:-
RAILS_GEM_VERSION = '2.1.1' unless defined? RAILS_GEM_VERSION

I would try RVM gemsets for this, because it's likely that you will need to keep separate sets of dependencies for each project as well as separate versions of Rails itself.
RVM seems generally a much tidier approach - using separate environments instead of a single pool of gems means that you can easily create or destroy environments without breaking others, and it reduces the risk of making a mistake when you specify the versions of dependencies.
In the Python world people use virtualenv to create completely separate environments for each of their projects, and I think that this is a good idea to copy.

In the config/environment.rb file look for RAILS_GEM_VERSION and set the value to the required version.
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
OR
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION

I would use Bundler: http://gembundler.com/
It's pretty easy to get used to, and has other benefits. It's by some of the Rails core members, and I believe will become the recommended solution soon after Rails 3.0.

To answer your original question, the underscore in your version number are used to specify a version when you create a new rails app. For example if you were to create a new rails app, rails would use the latest version installed in your system by default, but what if you wanted to use an older version installed on your system?
Here is the command...
rails 2.3.4 killerapp
You will need to have rails gem 2.3.4 installed on your system!
For that you can use rvm and gemsets to group your ruby/gems in convenient packages.
I hope this helps

Related

How to properly isolate multiple rails applications in development (local machine)

Given we use a preferred flavour of a ruby version manager (RVM or Rbenv) the ruby gems are isolated per ruby version.
Let's say we want to work on multiple applications locally (they are completely separate applications) and we want to use the same ruby version and the same rails version how do we properly isolate them and their gems? Are gemsets the (only) answer here? I mean if I have 5 applications with the same ruby version and I keep adding gems on all 5 fronts it's just a matter of time when one of the applications is ok with the latest stable gem version while one of the apps will still need to roll on an older version of the same gem due to legacy dependency or whatnot. How do you guys avoid this timebomb?
What if we want to use the same ruby version on multiple apps but a different Rails version? Rails being 'just a gem' is the answer same as for the above?
Thanks.
I'd recommend to use:
rbenv to handle multiple ruby versions on the same machine
bundler to define the dependencies of each application
Make sure to push .ruby_version, Gemfile and Gemfile.lock to make sure every is using the appropriate version of each gem...

How do I working on two apps with different Ruby/Rails versions?

I finished Michael Hartl's tutorial and created an app using Ruby 1.92 and Rails 3.2. Now I want to make my own app but I want to use the new versions of Ruby and Rails.
If I install the new versions for my new app, can I still make changes to my old app without things getting messed up or do I have to keep installing/uninstalling different Ruby and Rails versions?
You should look at using a ruby environment manager. I use rbenv, you can also specify in your gemfile which version of ruby and rails should be used for a specific application.
https://github.com/sstephenson/rbenv
You should be fine. Just make sure the proper versions are specified in your Gemfile and you will be good to go.
After installing the new version, by default new version will be used. However, already existing app will still use the version of rails they already exist as.
Bundler takes care a lot of the hard work for you in your situation.
Other notes:
Keep in mind RVM allows you to install "Gemsets". Should you have the need to use specific versions for gems you can create sets of them for use when needed.
You can also specify the version of rails when you're creating your app via rails _3.x.x_ new FooBar and you can identify what versions of rails are installed on your system via gem list --local | grep rails
If you need to negotiate local and global ruby versions, consider using rbenv.
https://github.com/sstephenson/rbenv
RVM is another popular option for ruby environment management.
https://rvm.io/
Personally, I have been more successful with rbenv.

Having multiple versions of Rails installed on OS X

OS X 10.6 has Rails 2.2.2 installed by default.
How to upgrade to Rails 2.3.8?
Since Rails 3 is out, is it possible to have multiple versions of Rails (like 2.3.8 and 3) installed on OS X?
(For example, I could be working on two Rails projects, one is Rails 2.x and the other is Rails 3.x).
Use rvm
RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems...
RVM allows users to deploy each project with its own completely self-contained and dedicated environment--from the specific version of ruby all the way down to the precise set of required gems to run the application. Having a precise set of gems also avoids the issue of version conflicts between projects, causing difficult-to-trace errors and hours of hair loss. With RVM, NO OTHER GEMS than those required are installed. This makes working with multiple complex applications where each has a long list of gem dependencies efficient. RVM allows us to easily test gem upgrades by switching to a new clean set of gems to test with while leaving our original set intact. It is flexible enough to even have a set of gems per environment or development branch--or even individual developer's taste...
I wouldn't bother with gemsets myself just do as other people have mentioned:
gem install -v=2.3.8 rails
and then to use:
rails _2.3.8_ new rails app
Note that this _versionnumber_ thing is a standard way of specifying a version of any rubygems installed executable.
Use
gem install -v=2.3.8 rails
where -v=[version number you want to install]
Then
list gem rails
In your config/environment.rb you can state which version of Rails you want to use.
To upgrade your OS version of the rails gem:
sudo gem install rails --version 2.3.8
While you're at it, you can also upgrade your OS rubygems system itself, overwriting the binary that OS X comes with. This might seem kind of hacky, but it's a stable thing that everyone does.
sudo gem install rubygems-update
sudo update_rubygems
Regarding managing rails versions in your projects, if your only needs are:
Specifying the version of rails that you want on a rails project
Specifying different versions of rails and/or other gems in each of multiple rails projects
Then rvm is overkill. The only thing you need, and what is also a complete and utter pleasure to work with, is Bundler. Bundler is like Software Update for your rails project. In fact, Rails 3 comes with Bundler by default, you just have to list the gems you need in Gemfile and it takes care of the rest. You don't need to worry about which versions of your gems are installed in the OS.
(You do however need the appropriate version of the rails gem installed in the OS in order to generate the rails project in the first place)
http://rubygems.org/
It's all you need =)

creating a new rails project in rails 2.2.2 (while having rails3)

In my computer (linux - ubuntu) I have installed rails 2.2.2 and rails 3
Now i want to create a project in rails 2.2.2. (Like rails ). But it asks to enter the rails command as in rails 3 (like rails new )
when i type rails -v it shows the rails version as 3. But in my gem list it has both rails 2.2.2 and 3
how can i create a project in rails 2.2.2
thanks in advance
cheers
sameera
Generally speaking if you have two versions of ruby and rails installed the last one will be the "normal" one using "normal" commands like:
ruby
rails
gem
For all the other versions you can define (or have defined during installation) some aliases like:
ruby187
rails222
gem222
Another solution could be this
Or the best solution I recommend for working with multiple ruby and rails versions is RVM
You can do this. Create a rails project.
rails myproject
Go to your envirnonment file and modify to
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
EDIT
Yes , Sorry about that .This method may have worked in rails versions < 3. My Bad ! RVM the way to go.
Should do the trick. Also I recommend you update to atleast Rails 2.3.9, in case you don't want to start off with Rails 3.
There are 3 easiest ways how to manage it:
0) remove not used version of rails
1) rails is just simple command, so each time when you need to run special version of it you can specify full full path to it( or alias it one time)
2) if you strongly need to have different types/versions of ruby environments( and/or different versions of ruby gems such as rails), I recomend you to use rvm - it is the most easiest way to manage it.
When you want to specifically use one version of your installed version for a specific project use
rails _2.2.2_ (your project)

Managing many Ruby on Rails applications of different versions

I'm learning Ruby on Rails with the AWDR book and have had to be specific about which version of Rails and Ruby that I am running on my local machine. I have just discovered that I need to roll back from ruby 1.8.7 to ruby 1.8.6 here. I also needed to roll back Rails to support the scaffold method so I could start the tutorial easily.
My question is: When I start contracting, developing and deploying projects in the real world, how am I going to manage all these different versions?
It looks to me like Rail's low tolerance for legacy code negates its ease of use philosophy! But I'm sure I'll grow to appreciate RoR.
As for Rails, What you can do is freezing your version, for example:
Make sure to install the proper Rails version, suppose you want version 2.2.2 : gem install rails v=2.2.2
Freeze and pack Rails with the project itself : rake rails:freeze:edge RELEASE=2.2.2
Now you will find Rails packed inside the vendor folder of your project, so you don't have to install Rails on the deploying machine.
And for Ruby, I like Ruby Version Manager(RVM), the easiest way to manage Ruby versions.
RubyGems is Ruby's package manager. You can install as many versions of gems (packages) as you want. You can install the latest by running sudo gem install rails (at the moment it will install 2.3.5). If you need 2.2.2, specify that with the -v or --version option: sudo gem install rails --version 2.2.2. Rails also installs a binary (yes, I know it's not really a binary file), rails, which generates a project. Because you have several versions of the gem, you need to control which binary gets called. When you install the rails gem, RubyGems puts a file in it's bin/ dir, which is a "link" to the real rails binary. That is the one you "call" when you say rails on the command line. However, all of the rubygems "link" binaries accept a parameter of it's own, which is what version you want to use. You would use the 2.2.2 rails binary like this:
rails _2.2.2_ my_project
I think the default is to use the most recent version, so if you want to use the most recent version, do this:
rails myproject
However, I see that you use 2.2.2 to get access to the scaffold method. I would strongly suggest you not to use that method, there's a reason for removing it. The scaffold method hides code, and makes customization hard. Instead, use the scaffold generator:
./script/generate scaffold --help
Good luck on your future rails adventures!
The latest version of Agile Web is written for 2.2.2 I believe. For this basic app they walk you through I'm very certain it should work with 2.3.x
The answer to the question for how you keep up is that you update your apps as needed and read the api and Changleogs to find out what has changed and fix the stuff that upgrades break. A great way to help with this is having a good test suite with good test coverage.

Resources