Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Created January 7, 2009 03:35
Show Gist options
  • Save copiousfreetime/44160 to your computer and use it in GitHub Desktop.
Save copiousfreetime/44160 to your computer and use it in GitHub Desktop.
# http://en.wikibooks.org/wiki/SQL_dialects_reference/Functions_and_expressions/Math_functions/Numeric_functions
def rand_function
@rand_function ||= (
if [ "SQLite", "PostgreSQL" ].include? ActiveRecord::Base.connection.adapter_name then
"random"
else
"rand"
end
)
end
# example
Model.find(:first, :order => "#{rand_function}()", :limit => 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment