Why does Rails 2.0.2 Load? - ruby-on-rails

Why does this happen?
ruby script/console
Loading development environment (Rails 2.0.2)
>> exit
jay-z-mac-pro:justinz$ rails -v
Rails 2.3.3

Look at your RAILS_ROOT/config/environment.rb file. In it you can set and lock the Rails gem version to load. Look for the line:
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
Change that to
RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION

Maybe your project is pinned to version 2.0.2. I haven't worked with Rails for quite some time, so I can't say for sure, but I believe this can be done in a config file.
Also, you should check if Rails is in your vendor directory (was that what it is called?). That's another way to force a specific version.

Check your config/environment.rb, it sets the Rails version for your application. It should be something like this:
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
script/console will load your application with the gems (including Rails) that you define in the config. rails is just the latest rails gem installed.
However, moving from Rails 2.0.2 to 2.3.3 is a pretty big change, you'll want to be careful about upgrading. Make sure to read the docs as it relates to your application.

Related

Programatic way to retrieve the Ruby version specified in the Gemfile?

I am looking for an efficient a programatic way to retrieve the Ruby version specified in a Rails app Gemfile.
# Gemfile
ruby '2.3.4'
You can use Gem.ruby_version
Gem.ruby_version.to_s
=> "2.2.5.319"

Rails does not load gems from vendor/gems

I have strange old buggy project on Rails 2.
It have gem's dependencies in config/environment.rb like
config.gem "andand"
config.gem "json"
config.gem "chronic"
config.gem "mini_fb"
all those gems are located in vendor/gems/
andand-1.3.3/
chronic-0.6.7/
json-1.7.3/
mini_fb-1.1.7/
rbet-1.0.3/
redis-3.0.1/
responsys_client-0.0.1/
but when i start unicorn server with this app it always complain that it can't find this gems. Why?
UPDATE
After building and installing gem from vendor/gems rails still complain about it.
I have tweake mini_fb gem into custom mini_fb_custom gem. Changed all references in gemspec and other files from mini_fb to mini_fb_my, installed it and it is shown in gem list as mini_fb_my. But it fails to load from config/environment.rb and complains that
Missing these required gems:
mini_fb_my >= 0
maybe i should rename lib/mini_fb.rb to lib/mini_fb_my.rb
i'll check it.
UPDATE 2
Yes, renaming files rocks!
You still need to install them from those folders, or unicorn will not know where to look for them.
Just install the gems from that directory and unicorn should pick them up.
UPDATE
You can install your gems locally with this command
gem install --local vendor/gems/gem/gem-name.gem
On more recent versions of rails you just specify path on the Gemfile
gem "gem-name", path: "path/to/gem"
My advice: replace the obsolete gem configuration with bundler (it works fine with rails 2, there should be a tutorial for rails 2 available on their website).
Configuration through gem command, freezing gems, etc. is just pain in the a** and it seemed kinda buggy to me when I'd used it (long time ago).

Strange behavior when setting rails version in environment.rb

I have rails 2.3.4 and rails 2.3.14 gems in my gem directory. Create a project
rails _2.3.4_ foo
Replace the line:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
with
if true
RAILS_GEM_VERSION = '2.3.4'
else
RAILS_GEM_VERSION = "2.3.14"
end
Now when I run the console, I get:
Loading development environment (Rails 2.3.14)
but if type in:
>> Rails.version
=> "2.3.4"
However, now I just add a comment:
if true
RAILS_GEM_VERSION = '2.3.4'
# comment
else
RAILS_GEM_VERSION = '2.3.14'
end
and I launch the console, and get:
Loading development environment (Rails 2.3.4)
Rails version = 2.3.4
Note that now it points to 2.3.4 in the "loading" line. Seems really bizarre to me that a comment will affect this.
The rails bootstrap code that determines which version of rails to use doesn't actually execute your environment.rb - it reads in that file and tries to extract what you set RAILS_GEM_VERSION to. That code is easily confused by conditionals and so on.
If you want precise control over what versions of gems are used I'd recommend using bundler.

Detect rails 2 app

I need to detect rack apps and rails 2 apps by simply inspecting the files in a project.
I've been able to do this for rack apps by checking for the existence of a config.ru file. Is there something similar I could check for in a rails 2 app?
From Phusion Passeger documentation: Phusion Passenger checks whether the virtual host is a Rails application by checking whether the following file exists:
dirname(DocumentRoot) + "/config/environment.rb"
This file exists also for rails 3 projects if that is a problem :/.
In config/environment.rb you can look for RAILS_GEM_VERSION = '2.x.x'
In Gemfile.lock for rails (2.x.x)
In Gemfile for gem 'rails', '2.x.x'
The file config/environment.rb should have a statement which mentions the required version of rails, e.g.:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION

In Rails, why sqlite works without config.gem 'sqlite3-ruby'

With a new Rails 2.3.10 project, the file config/environment.rb has the following line commented out:
# config.gem "sqlite3-ruby", :lib => "sqlite3"
but for some reason, I tried a scaffold foo, and start the rails server, and the app is running.
I thought the requirement is, every gem the app needs, it has to be listed in config/environment.rb?
In Rails 2.3, it's enough to have the gem installed on your system for you to use it.
In Rails 3, you must have the gem listed in your Gemfile and installed via bundler to use the gem.

Resources