Forum: Ruby on Rails sql Rand() returns same result

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A9f764247df3c88ccdd613d804eb8a73?d=identicon&s=25 jg (Guest)
on 2009-05-23 19:44
(Received via mailing list)
I have a method that calls Model.find(:first, :order => "RAND()")
twice. I want it to return two different results but it always returns
the same thing twice. Is there some kind of sql caching at work here?
If so, can I disable for this method?

thanks!

Jason
Ced56202e9039027cfc2da2f70457977?d=identicon&s=25 E. Litwin (Guest)
on 2009-05-24 07:07
(Received via mailing list)
First of all, I'm assuming you are calling rand() (lowercase).
rand() returns a value less than between 0 and 1.
Using this as the call to :order tells the database to use the column
reference (rather than a name).
In MySQL, if you pass a non-integer column value, it gets ignored.
(Not sure how other databases handle it).

I don't really understand what you are trying to randomize in your
order parameter.
You should probably get the count of the records in your table and
generate a random number between 1 and that count. Then use offset to
select that record.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-05-24 17:12
(Received via mailing list)
On Sat, May 23, 2009 at 10:06 PM, E. Litwin <elitwin@rocketmail.com>
wrote:

Speaking strictly from a MySQL perspective --

> First of all, I'm assuming you are calling rand() (lowercase).

Case isn't significant.

> I don't really understand what you are trying to randomize in your
> order parameter.
> You should probably get the count of the records in your table and
> generate a random number between 1 and that count. Then use offset to

? The syntax below works just fine to return a random instance of
Model.

>> I have a method that calls Model.find(:first, :order => "RAND()")
>> twice. I want it to return two different results but it always returns
>> the same thing twice.

What it *doesn't* do is guarantee that any two randomly selected
instances won't be the same :-)

Quick example:
>> (1..20).each do
?> @up = UserProfile.find(:first, :order => "rand()")
>> puts @up.id
>> end
8
8
11
1
5
1
13
13
2
6
17
5
4
9
6
17
8
10
7
5
=> 1..20

If the two really need to be different, you'll have to address that with
application logic.

FWIW,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-05-24 19:27
(Received via mailing list)
You may be getting stuck in the query cache. Try this:

Model.uncached { Model.find(:first, :order => "RAND()") }

Which will shut off query caching for that call.

--Matt Jones
Ced56202e9039027cfc2da2f70457977?d=identicon&s=25 E. Litwin (Guest)
on 2009-05-25 05:17
(Received via mailing list)
My bad - I didn't notice the quotes around "rand()" in the first
post.

I thought a random number was being passed to the database rather than
a function.

On May 24, 8:10 am, Hassan Schroeder <hassan.schroe...@gmail.com>
This topic is locked and can not be replied to.