Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Created September 18, 2011 18:19
Show Gist options
  • Save patrickgombert/1225346 to your computer and use it in GitHub Desktop.
Save patrickgombert/1225346 to your computer and use it in GitHub Desktop.
Better Singleton
class MailingList < ActiveRecord::Base
has_many :subscribers
before_create :assert_singleton!
def self.instance
first || create!
end
private
def assert_singleton!
return if self.class.count.zero?
raise "An instance of Mailing List already exists, use method instance"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment