user3410599
user3410599

Reputation: 45

Rails validates_uniqueness_of, logical AND

I have three paramaters, e.g. site_id_1 and site_id_2. and transport_distance

When creating a new record, rails should validate that site_id_1 AND site_id_2. dont already exist. (So the combination matters)

With validates_uniqueness_of(:site_id_1, :site_id_2) it says that site_id_1 or site_id_2 already exist. I've also tried: validates_uniqueness_of(:site_id_1 && :site_id_2) but it doesnt helped me.

Upvotes: 0

Views: 43

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12340

Rails 4 style:

validates :site_id_1, uniqueness: {scope: :site_id_2}

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51191

You should use scope param of validates_uniqueness_of validator:

validates_uniqueness_of :site_id_1, scope: :site_id_2

Upvotes: 4

Related Questions