如何检查记录是否存在



我有一个约会模型,我想验证是否存在具有相同日期和小时的记录。

class Appointment < ActiveRecord::Base
  attr_accessible :adate, :atime, :doctor_id  
  validates :adate, :presence => true     
  belongs_to :doctor
  validates_date :adate, :after => lambda { Date.current }  
  validate :appointment_uniqueness
  def appointment_uniqueness
      # if date (:adate) and hour(:atime) exists then
      # date is not available
  end
validates :adate, :uniqueness => { :scope => :atime }

见:http://guides.rubyonrails.org/active_record_validations_callbacks.html独特性

相关内容

  • 没有找到相关文章

最新更新