Topic: 2 .rand non-duplicate objects

i am trying to find 2 random objects, but this way allows for duplicates:

 @random_coupon_1 = Coupon.find(:all).rand
    @random_coupon_2 = Coupon.find(:all).rand

how can i make sure they are not the same object? (and find one that is different)


Re: 2 .rand non-duplicate objects

you could us ea loop until the record is different. There could be a rails way but nothing comes to my mind now.

Re: 2 .rand non-duplicate objects

Using SQL to your advantage doesn't require pulling all the records to memory (this example is for MySQL):

@random_coupon_1, @random_coupon_2 = Coupon.find(:all, :limit => 2, :order => "RAND()")

The syntax will vary according to the DBMS:

Re: 2 .rand non-duplicate objects