I am writing a rails 3.1 program that allows a user to 'share' a picture with other people via email. A user clicks 'share this with person X', which then sends an email out to person X (who doesn't have to be registered with my app) with a link that will take them to the picture:
http://myapp.com/pictures/uuid
The person clicks the link, is taken to my app and sees the picture.
This makes me think that my DB schema should have a Pictures table that has a UUID as a primary key, but I have not seen this done in Rails before (a开发者_JAVA技巧lways autoincrement ints). I don't think autoincrements will work here because it would be too easy for people to guess arbitrary url's and get to other peoples' pictures.
What is the best way to handle this in rails?
You should leave the primary key of the table alone and let rails autoincrement that.
I would create a string that is X random characters + id.to_s, and use that as the UUID. This way it's hard to guess and still guaranteed to be unique.
If you want your UUIDs to all be the same length, you could do a hash transformation on the resulting UUID, but then you'd want to do a check for uniqueness when saving to be sure.
精彩评论