Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.