开发者

What is the most efficient way to use Active Record to select one random record

开发者 https://www.devze.com 2023-03-14 17:39 出处:网络
Say I have开发者_运维技巧 100,000 Tweets. How can I use active record to very efficiently select just one Tweet?

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

精彩评论

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