Show Menu
Cheatography

Scopes Cheat Sheet (DRAFT) by

snippets for when creating scopes in Rails

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Scopes that query a DateTime column

scope :avail­able, -> { where(­ava­ila­ble_on: ..Time.cu­rre­nt.t­o_­date) }
scope :upcoming, -> { where(­"­sta­rts­_at­" > ?, Time.n­ow).or­der­("st­art­s_a­t")
scope :most_­rec­ent­ly_­added, -> { order(­cre­ate­d_at: :desc) }

Scopes that query a Boolean column

scope :admin, -> { where(­admin: true) }