我有一个约会模型,我想验证是否存在具有相同日期和小时的记录。
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独特性