1. Convert BSON::ObjectId to string
$profile = Profile.where(“profiles_manageds.profile_fname”=>’Admin’).first
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :077 >
1.9.3-p125 :078 >
1.9.3-p125 :079 > profile
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :080 > profile.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :081 >
1.9.3-p125 :082 >
1.9.3-p125 :083 > profile.id.to_s
=> “4fe969dd79216d0af9000002”
2. Convert string to BSON::ObjectId
$ profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :093 > profile_id = profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :094 >
1.9.3-p125 :095 >
1.9.3-p125 :096 > profile_id
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :097 >
1.9.3-p125 :098 >
1.9.3-p125 :099 > BSON::ObjectId.from_string(profile_id)
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
Like this:
Like Loading...