What is the most efficient way to use Active Record to select one random record
Say I have开发者_运维技巧 100,000 Tweets. How can I use active record to very efficiently select just one Tweet?
Tweet.all => [100K Records]
I want => Tweet.find_by_id[random] (something like this)
I would avoid selecting all and just build random into your query, something like this:
Tweet.find(:first, :order => "RAND()")
You can use this
Tweet.first(:order => "RAND()")
精彩评论