public
Created — forked from willnet/random_order.rb

monkey patch to add random_order to ActiveRecord with multi DB driver

  • Download Gist
random_order.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.