Édipo Féderle
Édipo Féderle

Reputation: 4257

About validates_uniqueness_of in Rails

i have a question, suppose that i have a model called Client, this model have a validation validates_uniqueness_of for attribute name, the problem is that the application is used by multiples users(use Client table), so lets say that a User 1 create a Client called Paul when other user try create a user with same same the validation will work, should however not.

I know if I did understand,

Thanks

Upvotes: 0

Views: 126

Answers (1)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

You can use :scope option:

:scope - One or more columns by which to limit the scope of the uniqueness constraint.

class Client < ActiveRecord::Base
  validates_uniqueness_of :name, :scope => :user_id
end

Upvotes: 1

Related Questions