Some Good Rails Scope Idioms

Submitted by swarut on Mon, 07/09/2012 - 18:40
  1.   scope :by, ->(user) { where(user_id: user.id) }
  2.   scope :followed_by, ->(user) { joins(:follows).where(follows: { user_id: user.id }) }
  3.   scope :reverse_chronological, order('created_at DESC')
  4.   scope :recent, ->(number=5) { reverse_chronological.limit(number) }
  5.   scope :with_role, lambda { |name| where(role: name) }
  6.   scope :founded, where(founder: true)