开发者

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()")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜