Random Records in Rails

Language: Ruby

# Via: http://almosteffortless.com/2007/12/04/random-records-in-rails/

class Widget < ActiveRecord::Base
 
  # ...
 
  def self.random
    ids = connection.select_all("SELECT id FROM widgets")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  end
 
end

# and then...

class SomeController < ApplicationController
 
  # ...
 
  def some_action
    @widget = Widget.random
  end
 
end
Reveal More
Added over 2 years ago by Costco_normal trevorturk

Refactorings

Re: Random Records in Rails

Refactoring of: Random Records in Rails

Language: Ruby

# Via: http://almosteffortless.com/2007/12/04/random-records-in-rails/

class Widget < ActiveRecord::Base
 
  # ...
 
  def self.random
    ids = connection.select_all("SELECT id FROM widgets")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  end
 
end

# and then...

class SomeController < ApplicationController
 
 
  def some_action
    @widget = Widget.random
  end
 
end
Reveal More
Added over 2 years ago by Picture_10_normal brupm

Re: Random Records in Rails, using max(id) instead of selecting all ids

Refactoring of: Random Records in Rails

Language: Ruby

# Via: http://almosteffortless.com/2007/12/04/random-records-in-rails/
# REFACTORED TO USE MAX(ID) INSTEAD OF RETURNING ALL IDS

class Widget < ActiveRecord::Base
 
  # ...
 
  def self.random
    # just get the max id, and use that as the seed for rand()
    max_id = connection.select_value("select max(id) from wigets")
    rand_id = rand(max_id)
    # find the first widget with an id equal to or greater than rand_id
    first(:conditions => "id >= #{rand_id}") || last
  end
 
end

# and then...

class SomeController < ApplicationController
 
 
  def some_action
    @widget = Widget.random
  end
 
end
Reveal More
Added over 2 years ago by Devo_normal gbuesing

Re: Random Records in Rails

Refactoring of: Random Records in Rails

Language: Ruby

class Widget < ActiveRecord::Base
 
  # ...
  # Only for mysql
  # RANDOM() will work for sqlite
  def self.random
    find(:first, :order => "RAND()", :limit => 1)
  end
 
end

# and then...

class SomeController < ApplicationController
 
  # ...
 
  def some_action
    @widget = Widget.random
  end
 
end
Reveal More
Added over 2 years ago by Twitterprofilephoto_normal maranh

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
Reveal More
Added over 2 years ago by Costco_normal trevorturk

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

Re: 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()")
  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 Default_profile_6_normal nanotechno