# Unique entries

**URL:** <https://www.ruby-forum.com/t/unique-entries/116996>\
**Category:** Rails\
**Created:** [October 11, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/unique-entries/116996 "2007-10-11T22:49:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 11, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/1 "2007-10-11T22:49:10Z")

</div>

I have this table associating airlines with rental cars. It has four  
columns - user\_id, airline\_id, rentalcar\_id and id. I need to make sure  
that the combo of airline\_id and rentalcar\_id is always unique - in  
other words, a user cannot have two fields with the same airline\_id and  
rentalcar\_id. Is this done with one of the validation commands in the  
model? I have look at the one that I can find and none seems to fit all  
that well. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 11, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/2 "2007-10-11T23:03:37Z")

</div>

> I have this table associating airlines with rental cars. It has four  
> columns - user\_id, airline\_id, rentalcar\_id and id. I need to make sure  
> that the combo of airline\_id and rentalcar\_id is always unique - in  
> other words, a user cannot have two fields with the same airline\_id and  
> rentalcar\_id. Is this done with one of the validation commands in the  
> model? I have look at the one that I can find and none seems to fit all  
> that well. Thanks,

Create a unique index on user\_id, airline\_id, rentalcar\_id in your  
migration… that would be one way

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 11, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/3 "2007-10-11T23:37:36Z")

</div>

You could try:

validates\_uniqueness\_of :rentalcar\_id, :scope =\> :airline\_id

Whichever is the widest scope (e.g., can an airline have a lot of  
rental cars? Or the other way around?) should probably go with  
:scope. I don’t _think_ it would affect anything, but for modeling’s  
sake it should be that way. 🙂

–Jeremy

On 10/11/07, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My books:  
Ruby in Practice

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book

> **[Site not found · DreamHost](http://www.humblelittlerubybook.com/)**
>
> The owner of this domain has not yet uploaded their website.

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 12, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/4 "2007-10-12T17:29:17Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I second that. I’m using validates\_uniquemess\_of with :scope for the  
> same purpose in one of my projects. In the testing I have done it did  
> not matter which one went in the :scope. I agree that the widest  
> would be the better choice for readability later.
> 
> On Oct 11, 5:36 pm, “Jeremy McAnally” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

What I was trying to do was to have a user with an airline membership be  
associated to as many rental car companies as they desired, but to  
insure the integrity of the database, I didn’t want an entry that had  
all three columns the same - user\_id, airline\_id, and rentalcar\_id. The  
same user\_id and airline\_id was ok as long as they all had different  
rentalcar\_id’s. Hope this makes some sense, because it was confusing the  
hell out of me. Anyway, here is what I did:

validates\_uniqueness\_of :rentalcar\_id, :scope =\> :airline\_id and  
:\_user\_id

and it seems to work. I didn’t know that I could say and but apperently  
I can. Thanks all,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 12, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/5 "2007-10-12T18:40:22Z")

</div>

On 10/12/07, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> validates\_uniqueness\_of :rentalcar\_id, :scope =\> :airline\_id and  
> :\_user\_id
> 
> and it seems to work. I didn’t know that I could say and but apperently  
> I can. Thanks all,

Well, you can “say” it, but Ruby will parse that as:

validates\_uniqueness\_of(:rentalcar\_id, :scope =\> :airline\_id) and  
:\_user\_id

The “and” part is a no-op, which I’m guessing isn’t what you intend.

If you want to use two columns for the scope, the syntax is:

:scope =\> [:airline\_id, :user\_id]

which is illustrated in the API docs for validates\_uniqueness\_of

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 12, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/unique-entries/116996/6 "2007-10-12T16:24:50Z")

</div>

I second that. I’m using validates\_uniquemess\_of with :scope for the  
same purpose in one of my projects. In the testing I have done it did  
not matter which one went in the :scope. I agree that the widest  
would be the better choice for readability later.

On Oct 11, 5:36 pm, “Jeremy McAnally” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
