Getting a random record using datamapper

Thursday 11/27/2008  –  Category: Uncategorized

Model.get(1+rand(Model.count))

You'll have to have dm-aggregates for Model.count to work correctly

One Response to “Getting a random record using datamapper”

  1. mmk Says:

    Simple, effective, but… If you delete records in that Model, then some of the IDs dont match exactly Model.count, thus this method can produce incorrect results, because rand can return IDs that no longer exist, etc.

Leave a Reply