Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created September 2, 2010 23:15
Show Gist options
  • Save jeffdeville/563133 to your computer and use it in GitHub Desktop.
Save jeffdeville/563133 to your computer and use it in GitHub Desktop.
scope :unrated_by, lambda {|user_id, limit|
# First, get a list of all of the products a user has already rated.
ids_to_exclude = Rating.only(:rateable_id).where(:user_id => user_id).collect{|r| r.rateable_id}
# I want to put the user in at a random point in the database to start rating products,
# to ensure that all products will have roughly the same number of ratings. Understanding the
# formula below isn't critical to understanding my problem.
total_idea_pool = GiftIdea.where(:sales_rank.lt => 1200).count - ids_to_exclude.size - limit
skip = total_idea_pool > 0 ? rand(total_idea_pool) : 0
# now load the products
GiftIdea:where => {:sales_rank.lt => 1200}.not_in(:_id => ids_to_exclude).skip(skip).limit(limit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment