Skip to content

Instantly share code, notes, and snippets.

@nuclearsandwich
Created December 9, 2011 19:51
Show Gist options
  • Save nuclearsandwich/1453011 to your computer and use it in GitHub Desktop.
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.
# 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