Skip to content

Instantly share code, notes, and snippets.

@sshaw
Created July 15, 2011 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshaw/1083952 to your computer and use it in GitHub Desktop.
Save sshaw/1083952 to your computer and use it in GitHub Desktop.
Rails 3 - select random record(s)
module RandomRecord
def random
order(random_function_name)
end
private
def random_function_name
if !defined? @__random_function_name
@__random_function_name = "rand()"
begin
connection.execute("select #{@__random_function_name}")
rescue ActiveRecord::StatementInvalid
@__random_function_name = "random()"
end
end
@__random_function_name
end
end
ActiveRecord::Base.extend RandomRecord if defined? ActiveRecord::Base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment