I am following this tutorial but I've hit a problem. I keep getting the error:
server.rb:10:in `<main>': uninitialized constant Mongo::MongoClient (NameError)
I am using the following gems:
Using bson 4.1.1
Using bson_ext 1.5.1
Using daemons 1.2.3
Using eventmachine 1.2.0.1
Using rack 1.6.4
Using tilt 2.0.5
Using bundler 1.12.5
Using mongo 2.2.5
Using rack-protection 1.5.3
Using thin 1.7.0
Using sinatra 1.4.7
My code:
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'mongo'
include Mongo
mongo_client = MongoClient.new("localhost", 27017)
I've required the mongo gem and included the Mongo class. Why isn't MongoClient being recognised? Why am I receiving the error?
Because MongoClient doesn't exist in that version of the mongo driver. As of the 2.0 version of the driver, the client class is Mongo::Client.
The 2.0 version introduced a number of big changes to the api, so I wouldn't recommend following a tutorial written for earlier versions - it looks like that page was last updated in Feb 2014. The mongodb docs have an up to date tutorial but for some reason the old tutorial is top of the google results.
Related
Using Anemone, I get this error when trying to use MongoDB:
gems/anemone-0.7.2/lib/anemone/storage.rb:28:in `MongoDB': uninitialized constant Mongo::Connection (NameError)
The code looks like this:
require 'anemone'
require 'mongo'
Anemone.crawl("my.site") do |anemone|
anemone.storage = Anemone::Storage.MongoDB
# more code here...
end
Any suggestions?
The Mongo storage driver in Anemone hasn't been updated since 2010 and still uses the syntax for the 1.x version of the Mongo ruby driver.
The easiest way to work around the problem is to instruct your script to use the older gem.
First, check if you have a 1.x version of the gem on your system. From the command line:
gem list mongo
It should give you something like:
*** LOCAL GEMS ***
mongo (2.2.5, 1.12.5)
mongodb (2.1.0)
I had a 1.x version (1.12.5), so I'm good to go. If you don't, you can install one using:
gem install mongo -v 1.12.5
Then, instruct your script to use the 1.x version of the gem by placing this line at the top of your file:
gem 'mongo', '=1.12.5'
Note, if you're using Mongo anywhere else in your script, it will now need to use the 1.x syntax.
Also, you may get a warning asking you to install bson_ext, which you can do with:
gem install bson_ext
Is there any gem available to Connect to JDBC DB2 for the Ruby project. I have come across activerecord-jdbc-adapter but it works only for Jruby.
Also can i use Jruby specific gem in Ruby? I have Ruby 2.2 and Jruby 9.0.0 in my machine
Did it using two other gems which was mentioned in one site.
We need Sequel and ibm_db gems and ran the below code, it worked
require 'rubygems'
require 'sequel'
require 'ibm_db'
Sequel.connect("ibmdb://#{username}:#{password}##{db_url}") do |db|
db.fetch(selectquery) do |row|
puts row
end
end
I am using activerecord and feedzirra(RSS parsing library). activerecord needs activesupport v3.2.8 and feedzirra needs v3.1.1. When I use both of them at the same time, I get error that they are conflicting because both of them require different versions of activesupport (3.2.8 vs 3.1.1.
What are my options?
Thanks
Use the latest feedzirra, it doesn't depend on 3.1.1 afaik:
gem 'feedzirra', :git => 'https://github.com/pauldix/feedzirra.git'
I'm connecting to Exasol using the dbi and ruby-odbc gems with unixodbc and it works fine, loaded like this:
require 'dbi'
require 'odbc_utf8'
When I add Bundler to the mix, ruby-odbc doesn't seem to be able to find the odbc configuration:
Could not load driver (undefined method `driver_name' for ODBC:Module)
/usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:318:in `rescue in load_driver'
/usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver'
/usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver'
/usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect'
It doesn't matter if I require it via Bundler.require (with gem 'ruby-odbc', require: 'odbc_utf8' in the Gemfile) or with require 'bundler/setup' and require 'odbc_utf8'.
This is how I try to connect:
DBI.connect('DBI:ODBC:exa', config['username'], config['password'])
In case it matters, this is what my .odbc.ini looks like:
[exa]
Driver = /opt/EXASolution_ODBC-4.0.8/lib32/libexaodbc-uo22.so
EXAHOST = 192.168.235.84..86:8563
edit: The way it works without Bundler only does so with Ruby 1.9.3. It looks to me like a scoping issue, as driver_name is a variable passed as an argument to the method that raises the exception.
Please try to install: gem install dbd-odbc
I'm running Ruby 1.9.2p180 on Ubuntu 11.04 64-bit.
I have the simplest possible CoffeeScript example using Sinatra which runs fine, but doesn't stop when I press the Ctrl+C shortcut. So every time I have to kill -9 and it's getting quite tedious.
The app.rb:
require 'sinatra'
require 'coffee-script'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
The views/app.coffee:
alert 'Foo'
It works on Ruby 1.8.7 with minor modifications:
require 'rubygems'
require 'sinatra'
require 'coffee-script'
require 'json'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
It also works on 1.9.2 when I remove the line require 'coffee-script', but gives me a warning:
WARN: tilt autoloading 'coffee_script' in a non thread-safe way; explicit require 'coffee_script' suggested.
Figured out that it works when using therubyracer, but fails on node. The version 0.1.97 from Ubuntu 10.10 repositories is the only exception. When using the 0.2.6 from Natty, it fails and also when installing the latest (0.4.8) using nvm, I wasn't able to install 0.1.97 via nvm.
Unable to replicate. I'm on a Mac (OS 10.6.7) running bash and Ruby 1.9.2p180. I don't get your tilt warning, either.
I also haven't experienced this problem when using The Middleman (Sinatra-based) or Rails 3.1; both use the same coffee-script gem (as does Tilt; I suspect the coffee_script is just a typo).
Have you tried updating all the pertinent gems (sinatra, coffee-script, tilt, execjs) to their latest versions? What JS environment do you have on your system (e.g. do you have node, or are you relying on therubyracer)?
On Ruby 1.9.2, Sinatra 1.3.2, CoffeeScript 2.2.0 and Node 0.6.2 this does not occur anymore.