Skip to content

Instantly share code, notes, and snippets.

@ollieglass
Created August 4, 2014 19:32
Show Gist options
  • Save ollieglass/00d029c866aa0a70bf26 to your computer and use it in GitHub Desktop.
Save ollieglass/00d029c866aa0a70bf26 to your computer and use it in GitHub Desktop.
Imagine I have tickets that belong to events. As a developer, I only want to access tickets that belong to an event, and I want to safeguard against accessing all tickets (contrived example, but anyway). If event ids start at 0, and I set the default scope to an event_id of -1, then if I forget to set an event (which would select all tickets), I…
class Ticket < ActiveRecord::Base
belongs_to :event
default_scope where(event_id: -1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment