Skip to content

Instantly share code, notes, and snippets.

@darthschmoo
Created May 8, 2010 19:51
Show Gist options
  • Save darthschmoo/394737 to your computer and use it in GitHub Desktop.
Save darthschmoo/394737 to your computer and use it in GitHub Desktop.
class Card < ActiveRecord::Base
named_scope :exclude, lambda { |ids|
{ :conditions => ["id NOT IN(?)", ids] }
}
# This approach doesn't work when chained to other scopes that limit the number
# of rows, because count returns a count of all rows.
# named_scope :random, lambda {
# { :offset => rand(count), :limit => 1 }
# }
# Takes an array with all the cards currently in play,
# returns a single card from the deck.
def self.random_from_deck( excludes = [] )
count = Card.count - excludes.length
exclude(excludes).find( :first, :offset => rand(count))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment