Monkey-patching a sample() method in Rails

It’s useful while debugging to choose a random sample. Thus you can include something like this in application.rb or an initializer.

Now you can say Post.sample to get a single post or Post.sample(5) to get 5 of them.

See also here and apparently the randumb gem will extract random ActiveRecords too.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>