Akuseru
Akuseru

Reputation: 1

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 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

Upvotes: 0

Views: 2385

Answers (3)

stef
stef

Reputation: 14268

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.

Upvotes: 3

Akuseru
Akuseru

Reputation: 1

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!

Upvotes: 0

Timon Vonk
Timon Vonk

Reputation: 449

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

Upvotes: 1

Related Questions