JZ.
JZ.

Reputation: 21877

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)

Upvotes: 2

Views: 136

Answers (2)

shajin
shajin

Reputation: 3264

You can use this
Tweet.first(:order => "RAND()")

Upvotes: 2

glortho
glortho

Reputation: 13198

I would avoid selecting all and just build random into your query, something like this:

Tweet.find(:first, :order => "RAND()")

Upvotes: 2

Related Questions