ruby-on-rails-4mongoid4

Mongoid 4 finding embedded documents by ID


I have a project that is my first serious dive into Mongoid.

I saw a tip to use the following command:

Parent.where('childrens._id' => Moped::BSON::ObjectId(params[:id])).first

But this doesn't work. Error message was:

NameError: uninitialized constant Moped::BSON

I found that BSON is no longer included, so I added it to my Gemfile, as well as Moped. Then, I did another fix I found (placing Moped::BSON=BSON in application.rb).

This still didn't work, but the error changed to:

NoMethodError: undefined method `ObjectId' for BSON:Module

So I am assuming that this method got deprecated or something. Does anyone have any other tips?

Just to be clear, I am finding myself in the situation where I want to sort embedded documents using jquery-sortable. This requires me to update them in the database, but the serialize from that doesn't include the parent document in the hash. So I figured I'd try to get it on the back end using an ID from the embedded document. That is why I need it.

Thanks again for any help you can provide.


Solution

  • I have solved the question though this won't be of much help to people in the future. The requirements have changed and now I am using human-readable strings as IDs to assist in friendly URLs and some other stuff.

    Therefore, I don't have any issues with ObjectIds. Cortex's solution should (from what I have read) work for dealing with ObjectIds but I cannot verify it now.