Super easy way to get a random model instance from an Active Record model.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitignore
Changelog.markdown
LICENSE
Readme.markdown
any.gemspec
init.rb

Readme.markdown

Any

Any provides a super easy way to get a random model instance from an Active Record model.. This is really handy when testing or populating your database with dummy data.

Usage

Say you have a model named Post and you want a random instance of it, all you have to do is:

Post.any

If you don't have any posts, it will return nil instead.

Due to the awesomeness of Rails 3, you can chain it to stuff like:

User.where("username != 'sam'").any

That would give you one random user who's username isn't sam. Pretty handy stuff.