Skip to content

Instantly share code, notes, and snippets.

@nsommer
Last active June 6, 2020 14:36
Show Gist options
  • Save nsommer/95256a0354cd69e3f60dc508c15dade6 to your computer and use it in GitHub Desktop.
Save nsommer/95256a0354cd69e3f60dc508c15dade6 to your computer and use it in GitHub Desktop.
class Article < ApplicationRecord
has_many :comments
has_many :tags
scope :draft, -> { where(published_at: nil) }
scope :published, -> { where.not(published_at: nil) }
scope :commented, -> { joins(:comments).distinct }
end
Article.published.count
# => 12
Article.published.commented.count
# => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment