Hi Dan,
Thanks for the reply. I have a few follow-up questions:
UUIDs seem to be very long, I was looking for a short url of about 6-8
characters, is there any way of doing this? (I still quite like the
quick and dirty rand(36**8).to_s(36) method)
> property :unique_value, UUID, :required => true, :unique =>
> true, :default => proc { UUIDTools::UUID.random_create }
snip
>create the underlying column so it is unique in the datastore
> (assuming it's supported)
What do you mean by '(assuming it's supported)'?
Using this method, what would happen if UUIDTools::UUID.random_create
created a string that had already been used. Would it just fail to
create the resource or try to create a new UUID?
thanks again for the help,
DAZ
On Mar 12, 6:18 pm, "Dan Kubb (dkubb)" <[email protected]> wrote:
> 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.