Search code examples
ruby-on-railsmongoidembedded-documents

mongoid get embedded documents parent by childrens id


I have two models

Class FirstModel
  embeds_one :second_model, class_name: 'V1::SecondModel'

Class SecondModel
  embedded_in :first_model, class_name 'V1::FirstModel'

I have some records of first model and I can get second model records from first models object in my rails c console as firstmodelobj.second_model

I have googled and tried enough but I can't seem to find the first model(parent) object from the second model(child) object's id.

Thanks in advance.


Solution

  • found it,

    V1::FirstModel.where("second_model._id" => Moped::BSON::ObjectId("id")).first