Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1384 (closed enhancement: wontfix)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Requesting random elements

Reported by: anonymous Assigned to: bitsweat
Priority: normal Milestone: 1.2
Component: ActiveRecord Version: 0.12.1
Severity: normal Keywords: random
Cc:

Description

We have:

Model.find( :all ) Model.find( :first ) etc.

How about Model.find( :random )? There have been a number of occassions where a random query would be useful. Ajaxed banner rotation, random quotes, random polls, random links, etc.

Attachments

svndiffforrandom.patch (7.0 kB) - added by anonymous on 05/23/06 01:31:25.
Patch for :order => :random. Oracle and OpenBase not yet supported.

Change History

06/01/05 17:46:31 changed by nzkoz

  • milestone set to 1.x.

I’m setting the milestone for this ticket to 1.x; this means that the earliest it will be implemented is after rails 1.0. If you feel this functionality should be included in the 1.0 release please email the developers list to discuss it.

Alternatively you could implement the functionality yourself and add a patch for it, that makes it much more likely to get included.

02/22/06 13:25:31 changed by anonymous

class Array

def any

at(rand(length))

end

end

Model.find(:all).any

This works for my simple needs, as fetching every instance from the DB isn't a big deal ATM.

05/23/06 01:31:25 changed by anonymous

  • attachment svndiffforrandom.patch added.

Patch for :order => :random. Oracle and OpenBase not yet supported.

07/07/06 23:22:09 changed by bitsweat

  • owner changed from David to bitsweat.
  • milestone changed from 1.1 to 1.2.

07/07/06 23:22:19 changed by bitsweat

  • summary changed from Requesting random elements to [PATCH] Requesting random elements.

08/24/06 10:32:28 changed by bitsweat

  • status changed from new to closed.
  • resolution set to wontfix.

order by rand() limit 1 is very taxing on the database. It shouldn't be a standard nor encouraged feature.

08/24/06 11:39:35 changed by jonathan.viney@gmail.com

This is how I query random records. {{{order by rand()}} is a lot faster when you are only selecting the id.

def self.find_random
  find(connection.select_values('select id from fans order by rand() limit 3'))
end