rails 3 validation on uniqueness on multiple attributes

后端 未结 3 1288
小蘑菇
小蘑菇 2020-12-02 04:52

I use Rails 3.0.0.beta4

I want to add a validation on uniqueness on two attributes, that means that my model is valid if the couple of \'recorded_at\' a

相关标签:
3条回答
  • 2020-12-02 05:26

    In Rails 2, I would have written:

    validates_uniqueness_of :zipcode, :scope => :recorded_at
    

    In Rails 3:

    validates :zipcode, :uniqueness => {:scope => :recorded_at}
    

    For multiple attributes:

    validates :zipcode, :uniqueness => {:scope => [:recorded_at, :something_else]}
    
    0 讨论(0)
  • 2020-12-02 05:34

    Dont work for me, need to put scope in plural

    validates_uniqueness_of :teacher_id, :scopes => [:semester_id, :class_id]

    0 讨论(0)
  • 2020-12-02 05:43

    Multiple Scope Parameters:

    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

    This should answer Greg's question.

    0 讨论(0)
提交回复
热议问题