now with probability weighting, and scoping!
License
jdar/find_random
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
FindRandom ========== Specify the probability that a record will be chosen from a given set of records. Cat.find_random( { 1 => 0.40, 2 => 0.40, 3 => 0.05, 4 => 0.05 }, :conditions => "hepness > 5" ) Probabilities normalized; they don't have to add to 1. Above, :conditions acts to filter the results. DUCK Cat.find_random # SAME Cat.find_random :first # SAME DUCK Cat.find_random :all, :limit=>5 # SAME Cat.find_random 5 # SAME DUCK Cat.find_random { 1=>1, # SAME 2=>1, 3=>1, 4=>2 } Cat.find_random [1,2,3,4,4] # SAME GOOSE! Cat.find_random [1,2,3,4] # ___ NOT SAME! Cat.hep.find_random [1,2,3,4] # __/ Yay! as with the :conditions in the initial example, named_scope can be used to filter records. Thus, if only records 1 & 2 have :hepness > 5, then #=> [#record1, #record2] Notes and Attribution ======= # My implementation hits the db 3 times, in ALL cases. # Tested on 2.2.0 # find_ids helper methods borrowed from http://blog.methodmissing.com/2007/4/24/partially-bypass-activerecord-instantiation-when-using-memcached/ # ... which will almost certainly collide with acts_as_ferret. Example ======= Example goes here. Todo ======= Write example "lucky guess" algorithm Write borrow/tests Calculate the performance benefit variables (is this always a good deal? When is it better to use :order=>'rand()' ?) Write an "allow_nil" option? (probably not.) Copyright (c) 2008 Darius Roberts, released under the MIT license
About
now with probability weighting, and scoping!
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published