Make your model uniq and identifiable through a readable name. Adds uuids to ActiveRecord models along with validators.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install universal_identifiable
Create a migration for your existing model with the built in generator.
rails g uuid Airport
In your model:
class Airport < ActiveRecord::Base include UniversalIdentifiable end
Set a uuid like you would with any other attribute.
airport = Airport.new(uuid: "dortmund")
Uuids are automatically prefixed with the modelname and namespaced with a dot. E.G: "airport.dortmund"
You have access to an unnamespaced uuid by passing the appropriate option:
airport.uuid(namespaced: false) => "dortmund" airport.uuid => "airport.dortmund"
Two validators are automatically added to your model:
validates :uuid, :presence => true, :uniqueness => true
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request