Skip to content

Instantly share code, notes, and snippets.

@samleb
Created April 16, 2010 14:58
Show Gist options
  • Select an option

  • Save samleb/368517 to your computer and use it in GitHub Desktop.

Select an option

Save samleb/368517 to your computer and use it in GitHub Desktop.
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
@samleb

samleb commented Apr 16, 2010

Copy link
Copy Markdown
Author

It obviously needs to be extended, not included, i.e. YourModelClass.extend(ActiveRecord::Random).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment