# Il.add\_index question

**URL:** <https://www.ruby-forum.com/t/il-add-index-question/177201>\
**Category:** Rails\
**Created:** [October 13, 2009, 10:06am UTC](https://www.ruby-forum.com/t/il-add-index-question/177201 "2009-10-13T10:06:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveb56](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@steveb56](https://www.ruby-forum.com/u/steveb56)\
**Post date:** [October 13, 2009, 10:06am UTC](https://www.ruby-forum.com/t/il-add-index-question/177201/1 "2009-10-13T10:06:51Z")

</div>

I have a table that is modeling a friendship between two players. I’d  
like it so that there can only be one record for this friendship in  
the table. I have added an index to the table to make sure that a  
record is unique like so …

id1 id2  
1 2  
1 2 # Error, record isn’t unique

add\_index(:friendships, [:friend\_id1, :friend\_id2], :unique =\> true)

I would also like to add an index to take care of the following  
situation…

id1 id2  
1 2  
2 1 # Error, since it’s the same as the first record

How would I do this?  
Thanks.  
Steve

---

<div class="post-metadata">

**Author:** ![steveb56](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@steveb56](https://www.ruby-forum.com/u/steveb56)\
**Post date:** [October 13, 2009, 10:23am UTC](https://www.ruby-forum.com/t/il-add-index-question/177201/2 "2009-10-13T10:23:49Z")

</div>

2009/10/13 steveb56 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> add\_index(:friendships, [:friend\_id1, :friend\_id2], :unique =\> true)
> 
> I would also like to add an index to take care of the following  
> situation…
> 
> id1 Â id2  
> 1 Â Â 2  
> 2 Â Â 1 Â # Error, since it’s the same as the first record

Could you setup your code so that for a friendship record id1 is  
always less than id2? If there is no way in the code that id1=2 and  
id2=1 can occur then you do not need to protect against it.

Colin

---

<div class="post-metadata">

**Author:** ![steveb56](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@steveb56](https://www.ruby-forum.com/u/steveb56)\
**Post date:** [October 13, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/il-add-index-question/177201/3 "2009-10-13T16:17:58Z")

</div>

Colin L. wrote:

> 2009/10/13 steveb56 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > add\_index(:friendships, [:friend\_id1, :friend\_id2], :unique =\> true)
> > 
> > I would also like to add an index to take care of the following  
> > situation…
> > 
> > id1 Â id2  
> > 1 Â Â 2  
> > 2 Â Â 1 Â # Error, since it’s the same as the first record
> 
> Could you setup your code so that for a friendship record id1 is  
> always less than id2? If there is no way in the code that id1=2 and  
> id2=1 can occur then you do not need to protect against it.

And if you really want to make this bulletproof, you can enforce id1 \<  
id2 with a check constraint in the DB.

> Colin

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![steveb56](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@steveb56](https://www.ruby-forum.com/u/steveb56)\
**Post date:** [October 15, 2009, 6:23am UTC](https://www.ruby-forum.com/t/il-add-index-question/177201/4 "2009-10-15T06:23:34Z")

</div>

Yeah that’s true, thanks.

Steve

On Tue, Oct 13, 2009 at 9:17 AM, Marnen Laibow-Koser \<

---

<div class="post-metadata">

**Author:** ![steveb56](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@steveb56](https://www.ruby-forum.com/u/steveb56)\
**Post date:** [October 13, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/il-add-index-question/177201/5 "2009-10-13T16:20:20Z")

</div>

I see what your saying yeah, that could work.Â Before I add it to the  
table, I would get it in the form id1 \< id2 & then my previous index  
would work the same way essentially catching both of the cases I was  
describing.

Thanks for the help.  
Steve
