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”
Leave a Reply
Recent Posts
- Marble Paint
(Friday 02/4/2011 – 1 Comment) - More Flickr Original Updates
(Sunday 01/23/2011 – 13 Comments) - Flickr Original updates
(Saturday 08/7/2010 – 19 Comments) - LED Light for iPhone 4
(Monday 06/28/2010 – 65 Comments)

October 26th, 2010 at 12:20 pm
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.