Overriding default id in mongoid

less than 1 minute read

This is an easy one, just want to make sure it's clear.

Add the following field to your model:


field :_id, type: Moped::BSON::ObjectId

And to allow setting it:

1 def id=(id)
2
self[:_id] = id
3
end


Now you can just do obj.id=[Some ObjectId] and save the object.


Leave a Comment