Get all the product ids: ids = Product.find(:all, :select => :id).map(&:id)
Choose six at random" random_ids = ... # left as an exercise for the reader
@products = Product.find(random_ids)
Get all the product ids: ids = Product.find(:all, :select => :id).map(&:id)
Choose six at random" random_ids = ... # left as an exercise for the reader
@products = Product.find(random_ids)
That will be incredibly inefficient. 'Select * from products order by rand() limit 6' is also not massively fast.
One fast way of doing this (which happens to be the way wikipedia does their random article search): assign to every product a random number between 0 and 1
When you want to get some random products, then pick a random number and select the products whose random value is great than the number you've just picked. If you've got an index on the column, this should be pretty speedy.
Fred
Well you have a column on the products table that contains a random number.
Fred
Really cool ...