Skip to content

Instantly share code, notes, and snippets.

@splashinn
splashinn / rails_or.rb
Last active May 1, 2020 21:50
Rails Using ActiveRecord's OR with scopes
class Project < ApplicationRecord
# We have 2 scopes, one for projects In Progress and one for projects Not Started
scope :in_progress, -> {where(project_stage: "In Progress")}
scope :not_started, -> {where(project_stage: "Not Started")}
# Say we want to combine the scopes so we can see projects
# that are in progress or that have been awarded, but not started
# This is where OR is handy :)
scope :project_status, -> {in_progress.or(not_started)}
end