Skip to content

Instantly share code, notes, and snippets.

@spencerldixon
Last active September 25, 2020 12:01
Show Gist options
  • Save spencerldixon/900bd759d89fdd32330c60ad3938f3e5 to your computer and use it in GitHub Desktop.
Save spencerldixon/900bd759d89fdd32330c60ad3938f3e5 to your computer and use it in GitHub Desktop.
Useful Scopes
# Recent things with optional period parameter
scope :recent, ->(period = 30.days) { where("created_at >= ?", period.ago.beginning_of_day) }
# With Active::Text :notes field
scope :with_notes, -> { left_joins(:rich_text_notes).where('action_text_rich_texts.id is NULL') }
# Between two optional dates (uses keyword arguments for optional params)
scope :between, ->(from: 30.days.ago, to: Date.current) { where(created_at: from.beginning_of_day..to.end_of_day) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment