Skip to content

Instantly share code, notes, and snippets.

@matt17r
Created September 5, 2025 00:23
Show Gist options
  • Select an option

  • Save matt17r/91bc7ddb2ab2a18f40866e3b40319ef0 to your computer and use it in GitHub Desktop.

Select an option

Save matt17r/91bc7ddb2ab2a18f40866e3b40319ef0 to your computer and use it in GitHub Desktop.
Time comparison... greater than == more recent than πŸ€¦β€β™‚οΈ
# I found this confusing to read... Active ones are ones that were read less than seven days ago so why is this a greater than symbol?
scope :active, -> { where("read_at IS NULL OR read_at > ?", 7.days.ago) }
# Of course! A date that is greater than another date is more recent than the other date πŸ€¦β€β™‚οΈ
scope :active, -> { where("read_at IS NULL OR read_at > ?", 7.days.ago) } # '>' == 'more recent than'
# But I think we can do better than [just] a comment (plus I already had an unread scope, why aren't I using it here???)
scope :unread, -> { where(read_at: nil) }
scope :read_within_past, ->(period) { where("read_at > ?", period.ago) } # '>' == 'more recent than'
scope :active, -> { unread.or(read_within_past(7.days)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment