Skip to content

Monkey patch to ActiveRecord to allow :order => :random that works the same for MySQL, SQLite and Postgres.

License

Notifications You must be signed in to change notification settings

Epictetus/active_record_random

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActiveRecordRandom

This plugin adds

:order => :random

to all the default ActiveRecord adapters. This means you can do this:

Post.find(:all, :order => :random, :limit => 10)

and it will work for MySQL, SQLite or Postgres without having to change the query. It also works for named scopes:

named_scope :randomized, :order => :random

Why?

Because MySQL has a different syntax than Postgres and SQLite for ordering by random. If you develop or test on SQLite and then deploy on MySQL, this is annoying. This plugin solves the problem.

Shouldn’t this be a patch to Rails?

I submitted it as a patch, but I’m such a loser that nobody gave me a +1. So for now it’s just a lowly plugin.

Copyright © 2009 Norman Clarke, released under the MIT license

About

Monkey patch to ActiveRecord to allow :order => :random that works the same for MySQL, SQLite and Postgres.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published