Creating random banner images

I needed to create random banner images for a project and have never been quite knowledgeable on the best way to do this.

I now have the following thanks to Agile Productions blog post on getting random data from the database with rails

module BannersHelper

  def random_banner
    # from http://www.agileproductions.com/articles/2007/11/25/a-better-way-to-select-random-rows-in-rails
    count = Banner.count
    banner = Banner.find(:first, :order => 'id', :offset => rand(count))
    return image_tag( banner.public_filename(), :height => 180 )
  end

end

Comments