How to create a sequential unique id for a db field
|Added at||2017-01-05 07:01|
I have the table Slug with the field url which is unique.
If I create
When I go to save, if Slug.url of "foo" already exists, I would like to then try for a Slug.url of "foo-1" if that also exists, try "foo-2" foo-3, foo-4, etc... until a value is found that doesn't exist and can be created in the db... What would be the right way to go about this in my rails model?
My latest code looks like this:
The problem with this code is the qUrl is picking up false positives, any time a title starts with the word "why" slugs are being found. Would love some help here to come up with something more reliable and elegant. Thanks