rails/rails — pull request #5321 by pfeiffer (2012-03-07 12:51:13+00:00)
Title: Add :conditions option to uniqueness validator

This commit adds a `:conditions` option to the uniqueness validator. The `:conditions` is added to the lookup used for determining the uniqueness of the attribute.

*Example use-case:*
An article with a `title` and a `status`. Status can be either `draft`, `published` or `archived`. We want to have unique titles - but only for non-archived articles. This cannot be accomplished using `:scope => :status`, since a draft article and a published article with identical titles both would be valid. This can now be accomplished with:

```ruby
class Article < AR::Base
validates :title, :uniqueness => {:conditions => ['status != ?', 'archived']}
end
```

In this case a draft article can have the same title as an archived article, but not the same name as a published article.

What do you think? Let me know if anything is missing - this is my first pull request to Rails.