Hi DAZ,

> I'd like to create some short urls (6-8 characters) that will act as a 
> look-up key in a database - so they have to be unique.

.. snip ..

> Both of these have a chance of not being unique (I think).

I would generally look at using a UUID instead of trying to roll my
own system for this. There a UUID type in dm-types, and you can define
the property so a UUID is auto-generated like this:

  property :unique_value, UUID, :required => true, :unique =>
true, :default => proc { UUIDTools::UUID.random_create }

> Is the best way to check if it is unique to use
> validates_uniqueness_of(:short_url) and then create the url again if
> it fails validation? If it is, what is the best way to code this
> (basically, generate a new short_url if the short_url is not unique).

I would recommend always adding :unique => true to the property, since
in addition to adding validates_uniqueness_of for the property it will
create the underlying column so it is unique in the datastore
(assuming it's supported).

If you care about data integrity you generally always want to make
sure that the constraints in the database are aligned as close as
possible with your model to ensure consistent behavior.

--

Dan
(dkubb)

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to