Strange behavior when setting rails version in environment.rb - ruby-on-rails

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.

Related

rails no such file to load -- ap (LoadError)

When I try any of the rails, rake command in staging server I got no such file to load -- ap (LoadError),
for rails s:
/usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in`require': no such file to load -- ap (LoadError)
xxxx
/config/application.rb:7
for rake -T:
rake aborted!
no such file to load -- ap
my application.rb file:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
my rails and ruby version:
rails version in local and staging: 3.0.11
ruby version in staging: ruby 1.8.7 (2012-02-08 patchlevel 358)
ruby version in local:ruby 1.8.7 (2011-06-30 patchlevel 352)
But I can run rails s, rails c, rake log:clear everything in my local machine. Gemfile file in staging is same as Gemfile in local.
what am I missing here? how can I resolve this?
In your Gemfile, change this line:
gem "awesome_print", :require => "ap"
...to this:
gem "awesome_print"
I just had the same thing happen to me. ap is a custom defined alias for awesome_print. This allows you to do things like ap Product.first and it will pretty print it for you. Much easier than typing out awesome_print Product.first
I'm not sure why, but when this happened to me, I dropped the alias, and it loaded without an issue. There is probably something else in play here that someone smarter than myself can explain, but I know that this worked for me.

Rails sitemap_generator Uninitialized Constant?

I'm trying to use the Rails site map_generator gem to generate site maps for a 8,000,00 page site. The gem can be found here: https://github.com/kjvarga/sitemap_generator
Here is my code in sitemap.rb:
require 'rubygems'
require 'sitemap_generator'
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "http://www.mysite.com"
SitemapGenerator::Sitemap.create do
add '/content.aspx?page=privacypolicy'
Product.find_each do |product|
add product_path(ppid), :lastmod => content.updated_at
end
end
However, when I run
>> ruby sitemap.rb
I get an error that says:
sitemap.rb:9:in `block in ': uninitialized constant
SitemapGenerator::Interpreter::Product (NameError)
However "Product" is the correct name of my model. Why is this happening?
I'm running Rails 3.1.2 and Ruby 1.9.
I'm the author of the gem. Better to open an issue on the GitHub page in future. SitemapGenerator does work in Rails 3 and Ruby 1.9.*. If you are running Rails, you don't need these lines:
require 'rubygems'
require 'sitemap_generator'
Also you generate your sitemaps by running Rake:
rake sitemap:refresh:no_ping
What is happening in your case is that because you're not running through Rake, the script does not know about the Product class, since your Rails environment hasn't been loaded.
Well, I wasn't able to get this gem working. My guess is that it doesn't work on Rails 3.1.2 or with Ruby 1.9. However, I was able to get another gem (big_sitemap) to work. Here is the link to it.
https://github.com/alexrabarts/big_sitemap

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

Why does Rails 2.0.2 Load?

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.

Why would yaml be unavailable in production?

On my OS X development system:
$ ruby --version
ruby 1.8.6 (2007-03-13 patchlevel 0) [universal-darwin8.0]
$ script/console
Loading development environment (Rails 2.3.4)
>> require 'yaml'
=> []
On CentOS 5.3 production system:
$ script/console production
Loading production environment (Rails 2.3.4)
/opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1959:in `method_missing':NoMethodError: undefined method `define_index' for #<Class:0x2e6f7b0>
>> require 'yaml'
=> false
Anything I can do about that NoMethodError?
Why would yaml be unavailable. Isn't it part of the core Ruby libraries?
Returning false means that the loading was successful or was already done. If it couldn't be loaded, it'd raise an exception instead.
It is part of the Ruby core libraries, but I'm not sure what gets bundled up with Ruby Enterprise Edition. Check that yaml.rb is inside your $LOAD_PATH. For example, try this in irb:
$LOAD_PATH.collect { |path| File.join(path, 'yaml.rb') }.find { |path| File.exist?(path) }
On OS X it will produce something like:
=> "/opt/local/lib/ruby/1.8/yaml.rb"

Resources