How can I use validation in Rails 3.1 to provide complex uniqueness?

I have a Customer model that belongs to brand. The client has only a name and an identifier (string) as attributes. I want to ensure the uniqueness of the name and identifier only in a particular brand. How can I apply this limited uniqueness?

+5
source share
1 answer

Use the :scopeconfirmation parameter : ActiveRecord::Validations#validates_uniqueness_of

validates_uniqueness_of :brand_id, :scope => [:name, :identifier]

As an alternative:

validates :brand_id, :uniqueness => {:scope => [:name, :identifer]}

In any case, it says: "for given nameand identifier brand_idshould be unique."

+13
source

All Articles