class EPaperSubscriber include Mongoid::Document include Mongoid::Timestamps field :email, type: String field :subscribed, type: Boolean, :default => true field :language field :last_paper_sent before_save do self.fix_email if self.class.where(:email=>self.email, :language=>self.language,:subscribed=>true).count != 0 return false end end def fix_email(save_flag=false) self.email = self.email.to_s.gsub(/[  ]/,'').sub(/;$/, '') self.save if save_flag end end