Created
December 9, 2011 19:51
-
-
Save nuclearsandwich/1453011 to your computer and use it in GitHub Desktop.
Question.. is there such a thing as a Database-leve #zip with ActiveRecord.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a hash of primary keys and scores such as: | |
scores = { 14 => 292.0, 11 => 999.0, 42 => 417.0 } # which is stored in Redis/calculated on the fly | |
# And an ActiveRecord model Model with a constraint. | |
class Model < ActiveRecord::Base | |
scope :passes_constraints, where(:has_constraint => nil) | |
end | |
# Say there are 50 items in `scores`, and the pass/fail constraint status of each is unknown. | |
# What is the most efficient way to take a 5-element slice of the models passing constraints in the order given by the score? | |
# For example, say I wanted a 1-element slice and ids 14 and 42 pass the constraint, I want | |
# => [Model[42]] | |
# back since it is the highest ranked constraint-passing model. | |
# Can this be done? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment