Skip to content

Instantly share code, notes, and snippets.

@norman
Created December 3, 2008 18:58
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 norman/31643 to your computer and use it in GitHub Desktop.
Save norman/31643 to your computer and use it in GitHub Desktop.
AR monkeypatch to allow :order => :random
module ActiveRecord
class Base
private
def self.add_order!(sql, order, scope = :auto)
scope = scope(:find) if :auto == scope
scoped_order = scope[:order] if scope
if order
sql << " ORDER BY #{connection.order(order)}"
sql << ", #{connection.order(scoped_order)}" if scoped_order
else
sql << " ORDER BY #{connection.order(scoped_order)}" if scoped_order
end
end
end
end
module ActiveRecord::ConnectionAdapters
class AbstractAdapter
def order(order)
case order
when :random then "RANDOM()"
else order
end
end
end
end
module ActiveRecord::ConnectionAdapters
class MysqlAdapter
def order(order)
case order
when :random then "RAND()"
else order
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment