Generate unique_id module
It's quite simple unique id module for mongoid:
module UniqueId
def unique_id(field)
instance_eval %Q{
before_validation do
self.#{field} ||= SecureRandom.uuid
end
}
end
end
Example:
class TestModel
include Mongoid::Document
field :test_id
extend UniqueId
unique_id :test_id
end
Written by Alexandr Korsak
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#