Skip to content

Instantly share code, notes, and snippets.

@Epictetus
Forked from willnet/random_order.rb
Created February 27, 2012 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epictetus/1926202 to your computer and use it in GitHub Desktop.
Save Epictetus/1926202 to your computer and use it in GitHub Desktop.
monkey patch to add random_order to ActiveRecord with multi DB driver
# for rails 3 or higher
module ActiveRecord
module Querying
delegate :random_order, :to => :scoped
end
module QueryMethods
def random_order
relation = clone
name = connection.adapter_name
case name
when 'SQLite', 'PostgreSQL'
relation.order_values += ['random()']
when /Mysql/
relation.order_values += ['rand()']
else
raise "random_order doesn't support this adapter"
end
relation
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment