forked from tilsammans/randomizr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b72d25d
commit e8380e2
Showing
3 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters