Rails 3: Validate combined values

Posted by Cimm on Stack Overflow See other posts from Stack Overflow or by Cimm
Published on 2010-03-28T15:54:25Z Indexed on 2010/03/28 16:03 UTC
Read the original article Hit count: 273

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

In the corresponding migration it could look like this:

add_index :family, [:husband, :wife], :unique => true

This would make sure the husband/wife combination is unique in the database. Now, in Rails 3 the validation syntax changed and the scope attribute seems to be gone. It now looks like:

validates :husband, :presence => true

Any idea how I can achieve the combined validation in Rails 3? The Rails 2.x validations still work in Rails 3 so I can still use the first example but it looks so "old", are there better ways?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails3