Rails 3: Validate combined values

前端 未结 1 1082
一生所求
一生所求 2021-01-31 02:44

In Rails 2.x you can use validations to make sure you have a unique combined value like this:

validates_uniqueness_of :husband, :scope => :wife
相关标签:
1条回答
  • 2021-01-31 03:26

    Bear with me. The way the validates method in ActiveModel works is to look for a Validator.

    :presence => true looks for PresenceValidator and passes the options: true to the validator's initializer.

    I think you want

    validates :husband, :presence => true, :uniqueness => {:scope => :wife}
    

    (The uniqueness validator is actually part of ActiveRecord, not ActiveModel. It's really interesting how the developers set this up. It's quite elegant.)

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