mercredi 4 mars 2015

Rails - how to fetch random records from an object?

I am doing something like this:



data = Model.where('something="something"')
random_data = data.rand(100..200)


returns:



NoMethodError (private method `rand' called for #<User::ActiveRecord_Relation:0x007fbab27d7ea8>):


Once I get this random data, I need to iterate through that data, like this:



random_data.each do |rd|
...


I know there's a way to fetch random data in MySQL, but I need to pick the random data like 400 times, so I think to load data once from database and 400 times to pick random number is more efficient than to run the query 400 times on MySQL.


But - how to get rid of that error?



NoMethodError (private method `rand' called for #<User::ActiveRecord_Relation:0x007fbab27d7ea8>):


Thank you in advance





Aucun commentaire:

Enregistrer un commentaire