views:

208

answers:

2

I'm writing an "action" for CloudCrowd which needs access to the Rails environment (for some ActiveRecord stuff) but the standard means of loading the environment is resulting in fishy errors.

I tried each of the following at the top of my action .rb file:

require(File.join(File.dirname(__FILE__), '../..', 'boot'))

and

require File.expand_path(File.dirname(__FILE__) + "/../../environment")

When I try to start the node I get this error:

»crowd node
Starting CloudCrowd Node on port 9063...
Missing the Rails 2.3.2 gem. Please `gem install -v=2.3.2 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.

And I of course do have the gem installed:

»gem list | grep -i rails
rails (2.3.4, 2.3.2, 2.2.2, 1.2.6)
A: 

Somebody from @documentcloud saw my plea and helped me work through it. Had to prefix the action script with this:

RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../../..'))
RAILS_ENV = ENV['RAILS_ENV'] || 'development'

if CloudCrowd.node?
  require 'rubygems'
  require 'activerecord'
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  require File.expand_path(File.join(File.dirname(__FILE__), '../..', 'environment'))
end
Teflon Ted
+1  A: 

Nice! I've actually had some trouble with your RAILS_ROOT path and replaced '../../..' with '../..'. Also, since you've already declared the RAILS_ROOT constant, you could chop off a few things in the environment requirement. Here's my version:

RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
RAILS_ENV = ENV['RAILS_ENV'] = ENV['RACK_ENV']

if CloudCrowd.node?
  require 'rubygems'
  require 'activerecord'
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  require "#{RAILS_ROOT}/config/environment"
  # and if you need to import 
  # anything from lib just go ahead and
  require 'my_custom_lib/name_of_file'
end
CaikeSouza