'Is it better practice to specify a Rails version or branch in a Gemfile?

I'm presently using

gem 'rails', '~> 5.0.0'

in my Gemfile.

I just learned of a different way to specify a Rails branch, which is:

gem 'rails', github: "rails/rails", branch: '5-0-stable'

Is there a best practice to include Rails in a Gemfile? Why would you use one way over the other?



Solution 1:[1]

gem 'rails', '~> 5.0.0'

Uses what is called pessimistic versioning. The so called squiggly arrow means that it will use any new minor version in 5.0.X this means that you still get bugfixes but can be pretty sure the library won't suddenly change on you.

~> 5.0.0 is equivalent to >= 5.0.0 <5.1.

Linking a gem directly to a github repo is only really a good idea if you want to be on the bleeding edge or if an major issue or incompatibility exists in the latest release but is fixed in the master or another branch/tag. Another common case is where you need to fork a gem.

Often the case is that you want to use gem A, but the latest release of gem B is incompatible with A which often happens right after a major version is released.

To link all your gems directly to their master branches would be very trying for your sanity. Bundler can't actually resolve the versions there so will be no help for doing dependency resolution.

Solution 2:[2]

There's no best practice that I know of.

The stable branch of rails changes constantly, it just tries to have no bugs. Using such a branch might be a good idea if you know that you want your app to target the next version of Rails.

The gem version from rubygems.org is probably the 'best practice' now that I think about it. You can guarantee that the Rails codebase won't change out under you.

No harm in trying the github approach for a few weeks if only to learn the hard way!

Good luck

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 stephenmurdoch