Re: Re: Random Records in Rails

Refactoring of: Random Records in Rails

Language: Ruby

# This code isn't database agnostic. 
 
  def self.random
    find(:first, :order => "RAND()", :limit => 1)
  end
 
# SQLite3::SQLException: no such function: RAND: SELECT * FROM "widgets"  ORDER BY RAND() LIMIT 1

# Flowcoder needs a comment system! <3
# I only work with mysql databases you could change it to RANDOM() for sqlite, but that won't help much since you probably have # a mysql / postsql production database. I'll edit my post to mention that.
Reveal More
Added over 2 years ago by Twitterprofilephoto_normal maranh