uninitialized constant Rails::Initializer (NameError) - ruby-on-rails-3

environment.rb:7: uninitialized constant Rails::Initializer (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/application.rb:78:in `require_environment!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/commands.rb:39
from script/rails:6:in `require'
from script/rails:6
I'm getting the preceding error, I'm trying to integrate salesforce with a rails app following the PDF here: http://blogs.developerforce.com/developer-relations/2011/02/a-brief-history-of-ruby-rails-with-the-forcecom-platform.html
I have made the instructions required, however it is required to edit the environment.rb
Here is my current version:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Newsletter::Application.initialize!
Rails::Initializer.run do |config|
config.gem "asf-soap-adapter", :lib => "activerecord-activesalesforce-adapter"
config.gem "asf-soap-adapter", :lib => 'asf-soap-adapter'
config.database_configuration_file = File.join(RAILS_ROOT, 'config','salesforce.yml')
config.time_zone = 'UTC'
end

I assume based on your tag that you are working in a Rails 3 app, the issue you have is that the environment.rb you have is Rails 2 specific. In Rails 3 you should be specifying your gems in your Gemfile, and removing the Rails::Initializer from your environment.rb.
Add this to your Gemfile:
gem 'asf-soap-adapter', :require =>'activerecord-activesalesforce-adapter'
There are some notes here on the upgrade from Rails 2 -> Rails 3.
http://rails3.community-tracker.com/permalinks/5/notes-from-the-field-upgrading-to-rails-3

I used the new Gem 'databasedotcom' for the same purpose which works perfectly
http://richardvanhook.github.com/databasedotcom-guide/#create_a_databasecom_account

Related

Rails Mocha gem - NoMethodError: undefined method `any_instance'

I'm using the Mocha gem in a Rails project to make stubs and bypass some live API calls.
I'm conforming to the documentation but keep getting an error.
In Gemfile (I use Bundler) I have:
gem 'minitest'
gem 'minitest-rails'
gem 'mocha', :require => false
In test_helper.rb:
require 'minitest'
require 'mocha/mini_test' # This is the last require statement
In product_controller_test.rb:
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
setup do
Product.any_instance.stubs({
manufacturer: "Acme"
})
end
end
The output is always NoMethodError: undefined method 'any_instance' for #<Class:blah>.
I have another Rails project where I can use any_instance successfully, so I've tried resolving by replicating the gems, require statements, etc. but with no success. I've also tried different permutations like require 'minitest/autorun', require "minitest/rails', and require 'mocha/setup'/require 'mocha', etc.
I'm on Rails 4.1.1 and using MiniTest 5.3.3 and Mocha 1.0.0.

Rails, Rack:SSL don't work on thin

I want to have https for entire application.
I decided to use this gem:
https://github.com/tobmatth/rack-ssl-enforcer
However after adding at end
gem 'rack-ssl-enforcer'
in Gemfile and
config.middleware.use Rack::SslEnforcer
in application.rb at end I get error:
/path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `send': undefined method `Rack' for #<App1::Application:0xf6dcb314> (NoMethodError)
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
from /path_to_app/www/stolikarnia/config/application.rb:57
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:21:in `require'
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:21
from script/rails:6:in `require'
from script/rails:6
As you see from logs i use Rails 3.0.9. I can't upgrade to RoR 3.1.0 because of dependencies.
In your Gemfile change the requirement to
gem 'rack-ssl-enforcer', :require => 'rack/ssl-enforcer'
Then, in your application.rb pass the class name as String to take advantage of lazy evaluation.
config.middleware.use "Rack::SslEnforcer"
As a side note, I encourage you to check https://github.com/josh/rack-ssl. It's the middleware introduced in Rails 3.1. You can already use it in Rails 3.0 with the additional benefit you won't need to change it once you'll upgrade to Rails 3.1.

Rails 3.0.9: config.active_record not found

I have upgraded from Rails 3.0.7 to Rails 3.0.9 and I am now getting the following error in my config/application.rb:
/Library/Ruby/Gems/1.8/gems/railties-3.1.0/lib/rails/railtie/configuration.rb:78:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x10107a830> (NoMethodError)
from /Users/mathias/ruby/myapp/config/application.rb:47
On line 47 of application.rb I have
config.active_record.schema_format = :sql
It seems that
config.active_record
is not found in Rails 3.0.9 where this worked in 3.0.7. This is regardless of which method I call on config.active_record. When I comment out any usage of config.active_record, the same occurs with config.active_mailer.
And I do a require "rails/all" in the application.rb
Any help appreciated.
Got this to work with 3.1.0 through the steps outlined here. The key bit that made most of my problems disappear was to change boot.rb to
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']

ruby resque without loading rails environment

I have a resque worker which works great but is just too slow. The main reason for this is I'm using activerecord and having to load the entire environment which takes at least 10-20 seconds just to load up (I don't keep a running worker at all times as I'm using Heroku and pay for the time the worker runs). I'm using a resque worker to grab & parse data from an external website and then dumping the data into my database.
My question is whether I should rewrite the method to not use Rails and instead use DataMapper? Or something else which would load faster than activerecord.
Or If I should extract the code (using ActiveRecord) which figures out what to do with the external data and move it out of the worker and back into the app?
Hope that makes sense.
I have the same problem.
you could setup your environment on the rake resque:setup rake task
I tried this. assuming my rake resque task is on lib/tasks/resque.rake
require "resque/tasks"
task "resque:setup" do
root_path = "#{File.dirname(__FILE__)}/../.."
db_config = YAML::load(File.open(File.join(root_path,'config','database.yml')))["development"]
ActiveRecord::Base.establish_connection(db_config)
require "#{root_path}/app/workers/photo_downloader.rb" #workers
#Dir.glob("#{root_path}/app/models/*").each { |r| puts r; require r } #require all model
require "#{root_path}/app/models/photo.rb" # require model individually
end
I haven't completely success beacuse I use the Paperclip gem which require rails environment
Rails’ bootstrap is really slow; it is intended to be kept running, until certain time for restart (to eliminate some memory leaks there most likely is, any software is not bug-free), and is not intended to be used as a site that is launched for one request and then shut down.
That kind of usage more resembles a script. If you need to launch it with browser, you can easily use something like Erubis to write the page and use ActiveRecord in the script (I think it was useable outside of rails) or similar abstraction layer. Myself, for small tasks, I just use Mysql2.
Use bundler to get active_record and other gem to you without rails application .
require 'rubygems'
require 'logger'
require 'active_record'
require 'bundler'
require "active_support"
require "spreadsheet"
require 'net/ping'
require 'net/http'
Bundler.setup
Bundler.require(:default) if defined?(Bundler)
$config_logger = Logger.new("./log/dev.log")
class Dbconnect
def initialize
#settings = YAML.load_file('./config/database.yml')["development"]
#adapter = #settings["adapter"] if #settings["adapter"]
#database = #settings["database"] if #settings["database"]
#pool = #settings["pool"] if #settings["pool"]
#timeout = #settings["timeout"] if #settings["timeout"]
end
def connect_to_db
ActiveRecord::Base.establish_connection(
:adapter => #adapter,
:database => #database,
:reconnect => #reconnect,
:pool => #pool,
:timeout => #timeout)
$config_logger.info "\n db Connected: to => #{#database} "
end
end
end
}
Example Gemfile :
source "http://rubygems.org"
gem 'mail'
gem "escape_utils"
gem 'json',:require => "json"
gem 'json_pure'
gem 'resque'
gem 'resque-scheduler'
gem 'redis-namespace'
gem 'resque-status'
gem 'rake'
gem 'em-udns'
gem 'sqlite3'
gem 'spreadsheet'
gem 'activerecord', '3.2.1', :require => "active_record"
gem 'net-scp', :require => 'net/scp'
gem 'net-sftp', :require => 'net/sftp'
gem 'net-ssh', :require => 'net/ssh'
gem 'dir'
gem 'amatch'
gem 'haml'
gem 'net-ping'
gem install bundler
rest of the thing : bundle install .

Webmock gem in rails 3 and properly including it

I'm likely doing something very simply wrong, but I'm not quite sure what it is. I am porting a rails 2 application to rails 3. This application uses webmock for a bunch of it's tests.
If I include
gem 'webmock'
In my Gemfile, the tests pass, but when I start the server and run the app locally, hitting a controller that should make a web call throws an error:
WebMock::NetConnectNotAllowedError
If I do NOT include the line in my Gemfile, then when I run the app locally, it works fine, but the tests error out with:
`require': no such file to load -- webmock (LoadError)
When this line is hit in my test_helper.rb
require 'webmock'
I'm guessing I've got something configured wrong, but I haven't hit the right google incantation to shed any light on it yet. Where I did I go astray?
Thank you.
Try telling your Gemfile to only load webmock when you're in a test environment:
group :test do
gem "webmock"
end
On my Ruby 1.9 Rails 3 instance I have something like the following:
group :test do
gem "mocha"
gem "webmock"
end
group :development do
gem 'ruby-debug19', :require => 'ruby-debug'
end