Ruby NameError with BSON - ruby

I am new to ruby so I am sorry if this question has an obvious answer but I have not had much luck with this.
But I keep running into
uninitialized constant BSON::ObjectID (NameError)
I have the require
require 'mongo'
Then here is the piece of code that's throwing the error, collection of course points to a db.
#Insert and return the row.
def insert(row)
id = collection.insert row
collection.find_one(:_id => BSON::ObjectID.from_string(id.to_s))
end
I am totally at a loss for whats wrong here. seeing how ruby doesn't like me requiring bson before or after mongo.
Tried in 1.9.2 and 1.8.7

Update: It's Id (upper, then lower case), not ID.
Just to be sure about it, put the require 'mongo' in the same file as the code block above, and then change BSON::ObjectID to ::BSON::ObjectId.

Try a require 'bson' in the top of your file.

Was not able to 100% solve the issue, but id was already a BSON::ObjectID so I just changed it to
collection.find_one(:_id => id)
Works as I would like it to now!

Related

How to use Bigquery streaming insertall on Ruby Rails

EDIT: Fixed - for ruby use "insert_all" instead of "insertAll" like the api specifies. The api for ruby needs updating.
Im using v 0.6.4 of the google-api-client gem and trying to create a streaming insert, but keep getting the following error:
google_bigquery.rb:233:in undefined method `insertAll' for #<Google::APIClient::Resource:0xcbc974 NAME:tabledata> (NoMethodError)
My code is as follows:
def streaming_insert_data_in_table(table, dataset=DATASET)
body = {"rows"=>[
{"json"=> {"person_id"=>1, "name"=>"john"}},
{"json"=> {"person_id"=>2, "name"=>"doe"}},
]}
result = #client.execute(
:api_method=> #bigquery.tabledata.insert_all,
:parameters=> {
:projectId=> #project_id.to_s,
:datasetId=> dataset,
:tableId=>table},
:body_object=>body,
)
puts result.body
end
Could someone tell me if the insetAll has been created for the google-api-client gem? I have tried 'insert' as that is what table, dataset etc use and get the same error as well.. I can however run tabledata.list perfectly fine.. I've tried digging throught the gem source code and didn't get anywhere with that.
Is the body object that I created correct or do I need to alter it?
Any help is much appreciated.
Thanks in advance and have a great day.
Ok. So fixed it and updated the code in the question. For ruby: the method is called "insert_all". Also note that the table & schema must be created BEFORE the insert_all. This id different when compared to the the "jobs.insert" method which will create the table if it doesn't exist

Most appropriate place to require library in Padrino/Sinatra

I'm using "therubyracer" in a model, and I'm requiring at the top of the model as so:
require 'v8'
class Thing
def self.ctx; ##ctx ||= V8::Context.new; end;
def self.eval(script); ctx.eval(script); end;
end
However, I intermittently get:
NameError - uninitialized constant Thing::V8:
/app/thing.rb:3:in `ctx'
When testing requests through a local Padrino server, apparently after I modify code in Thing. This is corrected by restarting the padrino server. I'm assuming requiring v8 somewhere else would fix this problem, wheres the correct place?
This looks like it might be caused by the Padrino reloader getting confused when it reloads your thing.rb file, causing Ruby to look for V8 in the Thing namespace.
Try explicitly specifying V8 is in the top level using the :: prefix:
def self.ctx; ##ctx ||= ::V8::Context.new; end;
You can put it wherever you want if you add it on the Gemfile. Did you added it?
Thanks!

How to get the journal/notes for a Redmine issue in Ruby?

I am using the redmine_client (0.0.1) gem to lookup issues in Redmine but get an error when I do something like this:
puts issue.journals.inspect # error - undefined method `journals' for #
I am following this example: https://gist.github.com/552610
Any idea what might be the problem? All the other fields work fine except for journal.
Journals is an association to an Array of Journal objects. I don't know if I've added Journal support to that gem yet (I don't use it much anymore). Try forking the gem and adding a new journal.rb class like issue.rb.

Requiring gem in Rails 3 Controller failing with "Constant Missing"

I've seen this asked a few times in other threads, but none of the answers seem to apply.
Environment:
Rails 3
amazon/ecs gem from jugend. The lone file is here:
http://github.com/jugend/amazon-ecs/blob/master/lib/amazon/ecs.rb
my gemfile has:
gem 'amazon-ecs', :git => 'git://github.com/jugend/amazon-ecs.git'
Everything works in irb. I can run:
bundle console
require 'amazon/ecs' and then go to town
when I try to use it from the controller though, like so:
require 'amazon/ecs'
require 'amazon/ecs'
class SearchController < ApplicationController
def index
end
def results
Amazon::Ecs.configure do |options|
options[:aWS_access_key_id] = '[key]'
options[:aWS_secret_key] = '[secret]'
end
res = Amazon::Ecs.item_search(params[:search], {:response_group => 'Medium', :search_index => 'All'})
end
end
I get: uninitialized constant SearchController::Amazon at line 8, where I first try to use Amazon.
the ecs.rb has a module Amazon containing a class Ecs. I'm not sure why this is working in erb, and not in rails.
I'm still kinda new to Rails, so please answer using small words. :-/
Was given the answer. I moved my initialization code to an initializer in config/initializers file, removed the require entirely, and things worked. I don't know why though, so if someone could answer that, that'd be great.
All of the gems require their files by default, so usually you don't need to explicitly require any files.
Speaking about your problem, it could somehow be, that your controller is run before Amazon module is processed.

undefined local variable or method 'within' for User:class

I am working with a book to teach myself Ruby-on-Rails. Ruby version is 1.2.3 and rubygems V 1.3.5.
I start the console by ruby script/console and enter:
user = User.new(:screen_name => "example",
?> :email => "exampleATexample.com",
?> :password => "example")
but instead of adding the data to the DB, I get the following:
NameError: undefined local variable or method 'within' for User:Class from D:/ruby/lib/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1235:in 'method_missing' from ./script/../config/../config/../app/modules/user.rb:13
I don't really understand what's going on. Any kind of help is much appreciated, thank you!
First, I believe you main Rails v 1.2.3, this is a very old version of Rails, don't use it.
Second thing, the command you entered has wrong Ruby syntax... try this instead:
user =User.new(:screen_name => "example", :email => "exampleATexample.com", :password => "example")
Third thing: please paste more code....
Also, just as a check.
Whenever something doesn't work in Rails that has to do with the database, make sure you run
rake db:migrate
Sometimes this can slip your mind and cause weird errors.
Also, just as a side note... I would make sure that you add some validations to your user model that requires a user to confirm their password and or email. It's generally a good practice.
validates_confirmation_of :password, :email
All the best with learning Rails. Make sure you take #dvyjones's advice and upgrade to the latest version of Rails... a lot has changed since v. 1.2.3

Resources