Skip to content

Commit

Permalink
add teh codez
Browse files Browse the repository at this point in the history
  • Loading branch information
tilsammans committed Aug 3, 2011
1 parent b72d25d commit e8380e2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
= Randomizr

Returns one random Active Record object using cross-platform ANSI compliant SQL.

We have all used <tt>order by rand()</tt> to select a random record from time to time.
We know we shouldn't, but it was just too easy. Well no more. NO MORE. It's
now +easier+ to do it properly by just using this gem. That's right. It will
solve all your ansi-sql troubles and put that smile back on your face.

== Prerequisites

Active Record, of course. Any version 2 or 3 will do.

== Installation

config.gem 'randomizr'

or in your Gemfile:

gem 'randomizr'

I told you installation would be easy.


== Examples

Comment.randomizr # => look ma, a random comment!

Copyright (c) 2011 Joost Baaij, released under the MIT license
12 changes: 9 additions & 3 deletions lib/randomizr.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require "randomizr/version"
require "activerecord"

module Randomizr
# Your code goes here...
end
# http://paulbarry.com/articles/2009/08/30/active-record-random
class ActiveRecord::Base
def self.randomizr
if (c = count) > 0
first(:offset => rand(c))
end
end
end
2 changes: 2 additions & 0 deletions randomizr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Gem::Specification.new do |s|
s.description = %q{Returns one random Active Record object using cross-platform ANSI compliant SQL}

s.rubyforge_project = "randomizr"

s.add_dependency('activerecord', '>= 2.1', '< 4')

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit e8380e2

Please sign in to comment.