Created
April 16, 2010 14:58
-
-
Save samleb/368517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module ActiveRecord | |
| module Random | |
| def random | |
| select_max_pk = select("MAX(#{primary_key})").to_sql | |
| where("#{primary_key} > ? * (#{select_max_pk})", rand).first | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It obviously needs to be
extended, notincluded, i.e.YourModelClass.extend(ActiveRecord::Random).