class SpudCalendar < ActiveRecord::Base has_many :spud_calendar_events, :dependent => :destroy validates_presence_of :title, :color validates :identifier, :presence => true, :uniqueness => true before_validation :set_identifier scope :with_identifier, ->(identifier){ where(:identifier => identifier).first } private def set_identifier if identifier.nil? && title.present? self.identifier = title.parameterize.underscore end end end