Answers:
在Rails 2中,我会写:
validates_uniqueness_of :zipcode, :scope => :recorded_at
在Rails 3中:
validates :zipcode, :uniqueness => {:scope => :recorded_at}
对于多个属性:
validates :zipcode, :uniqueness => {:scope => [:recorded_at, :something_else]}
validates :zipcode, :uniqueness => {:scope => [:recorded_at, :something_else]}
吗?
:scope
外键,您需要使用:fkey_id
的符号,而不是:fkey
那些,即使是“基本” :uniqueness
适用于:fkey
, :message => ' is taken for this recorded date'
class TeacherSchedule < ActiveRecord::Base
validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]
end
http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of
这应该回答格雷格的问题。
recorded_at
在邮政编码范围内唯一。validate :recorded_at, : uniqueness => { :scope => :zipcode }