Ruby NameError with BSON
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 what开发者_开发问答s 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!
精彩评论