Created
May 8, 2010 19:51
-
-
Save darthschmoo/394737 to your computer and use it in GitHub Desktop.
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
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