# Validates uniqueness two columns both ways?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372>\
**Category:** Rails\
**Created:** [March 10, 2011, 8:11pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372 "2011-03-10T20:11:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 10, 2011, 8:11pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/1 "2011-03-10T20:11:01Z")

</div>

Hello!

I’m trying to figure out how to make two columns unique regardless of  
order. If order mattered, I could scope the validates\_uniqueness\_of,  
like this:

> <https://stackoverflow.com/questions/1633297/how-do-i-validate-two-fields-for-uniqueness>

But that’s only one way. What if I have two columns, called “sender”  
and “receiver” and it doesn’t really matter who is the sender and who  
is the receiver as long as that “grouping” doesn’t exist previously.

For example, assume I have this already in the database:  
Sender = 1, Receiver = 2

Then, the following should occur:  
Sender = 1, Receiver = 3 # =\> PASS  
Sender = 1, Receiver = 2 # =\> FAIL  
Sender = 2, Receiver = 1 # =\> FAIL  
Sender = 3, Receiver = 1 # =\> FAIL (if the first one that passed  
already happened)

I tried this:

validates\_uniqueness\_of :sender\_id, :scope =\> :receiver\_id  
validates\_uniqueness\_of :receiver\_id, :scope =\> :sender\_id

But that doesn’t quite do it because they aren’t tied together. I can  
custom-validate this but I was wondering if this pattern has already  
been accounted for somehow.

Any pointers/references?

Thanks!

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 11, 2011, 1:27am UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/2 "2011-03-11T01:27:28Z")

</div>

Two ideas:

1. Create two unique database indices, one each for each pairing order  
and catch the exception on save on create.

2. Do it manually.

errors.add(:base, ‘already exists’) if  
(Klass.where(:sender\_id =\> sender.id, :receiver\_id =\> receiver.id) +  
Klass.where(:sender\_id =\> receiver.id, :receiver\_id =\>  
sender.id)).any?

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 11, 2011, 3:42am UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/3 "2011-03-11T03:42:36Z")

</div>

Thanks, Martin!

I’ve headed down path #2, but I thought of posting here just in case  
there already existed a custom validation. Maybe this isn’t a very  
common thing. Seems to be that usually it’s explicit in the direction  
(i.e. “husband =\> wife” relationship or “employer” =\> “employee” or  
something like that).

-Danimal

On Mar 10, 7:26pm, Martin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 11, 2011, 11:09am UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/4 "2011-03-11T11:09:49Z")

</div>

You could alway store them in known order so that  
sender.id \<= reciver.id  
and use scoped validates\_uniqueness\_of.

Assuming that sender and reciver can be exchanged in this relationship  
and does not have any additional meaning.

Like ‘friendship’ when it does not matter who is friend A and who is  
friend B

Robert Pankowecki

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 11, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/5 "2011-03-11T13:48:39Z")

</div>

Of course, on further thought,

The downside to this ID reordering is that I’d lose the data about who  
did the request.

For the ‘friendship’, it doesn’t matter. I.e. Person 1 is connected to  
Person 2 is exactly the same functionally as Person 2 is connected to  
Person 1 (in my app, that is). But there may be value in knowing which  
person started the process (i.e. the “sender”).

Hmmm… I guess a custom validation it is.

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 12, 2011, 10:28am UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/6 "2011-03-12T10:28:24Z")

</div>

How about a little trick. Make Id column of the table a string and  
then:

class Friendship \< ActiveRecord::Base

belongs\_to :sender, …  
belongs\_to :requester, …

before\_validation :standarize\_id, :on =\> :create  
validates\_uniqueness\_of :id

def standarize\_id  
self.id = [sender.id, requester.id].sort.join(",")  
end

end

This way we don’t forget the information about whose is the sender and  
who is requester. Instead we relay on standarized unique id for the  
the couple.

Robert Pankowecki

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [March 11, 2011, 1:39pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-two-columns-both-ways/204372/7 "2011-03-11T13:39:53Z")

</div>

Hi Robert! Thanks for the post.

Your thought would work, except that it’s very much like ‘friendship’  
in that it’s immaterial who requested the ‘friendship’. And that’s the  
crux of it. Since Person 1 or Person 2 could be the sender/requester,  
it has to check both ways.

Then again, I just had a thought. Perhaps given two people, the lower  
ID is always set as the sender and the other as the receiver. Hmmm…  
then, the order would always be known.

If Person 2 requests friendship with Person 1, the friendship object  
is saved with sender=\>1, receiver=\>2  
Then, if later Person 1 requests friendship with Person 2, it’s saved  
as sender=\>1, receiver=\>2 and it would fail validation.

Interesting… I need to noodle on that a bit more and see if that  
would work thoroughly.

Thanks!

-Danimal

On Mar 11, 5:08am, “Robert Pankowecki (rupert)”
