finding random instances of a model

I find myself doing this somewhat often, so I thought I’d share.

Assuming you have a User model and you want to find a random user:

u = User.find(1 + rand(User.all.count))

Sometimes this returns a RecordNotFound if the user has been deleted; otherwise, it’s a handy code snippet.