0

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

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
4

1 回答 1

1
validates :adate, :uniqueness => { :scope => :atime }

见: http: //guides.rubyonrails.org/active_record_validations_callbacks.html#uniqueness

于 2013-01-20T02:45:17.230 回答