Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Created July 5, 2011 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otobrglez/1064708 to your computer and use it in GitHub Desktop.
Save otobrglez/1064708 to your computer and use it in GitHub Desktop.
Random banners for specific position with random order
# Show banners for fixed position with some visibility and date range limits.
# Also return banners in random order.
# Use like so:
# Banner.for_position('a').each do { |b| b... }
# Tested with postgresql, sqllite and rails 3.1...
def self.for_position(position = 'a', for_date=DateTime.now.to_date)
where("position = ?", position)
.where("? between from_date AND to_date", for_date)
.where("hidden = 0")
.order("RANDOM()")
.limit(
[(2 if position=='a'),
(4 if position=='b'),
(1 if position=='c')].compact.first
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment