# Newbie : Easiest way to validate uniqueness of 2 fields that are related

**URL:** <https://www.ruby-forum.com/t/newbie-easiest-way-to-validate-uniqueness-of-2-fields-that-are-related/141096>\
**Category:** Rails\
**Created:** [June 22, 2008, 12:41am UTC](https://www.ruby-forum.com/t/newbie-easiest-way-to-validate-uniqueness-of-2-fields-that-are-related/141096 "2008-06-22T00:41:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidPaquet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@DavidPaquet](https://www.ruby-forum.com/u/DavidPaquet)\
**Post date:** [June 22, 2008, 12:41am UTC](https://www.ruby-forum.com/t/newbie-easiest-way-to-validate-uniqueness-of-2-fields-that-are-related/141096/1 "2008-06-22T00:41:01Z")

</div>

Hello everyone

Just to be sure, what is the easiest way to validate that a  
combination of 2 fields is unique in my model ?

Example :

I have Posts and Comments

A posts can have multiple comments (so post\_id is not unique) and  
multiple comments can have the same title (title is not unique  
either).

But, I don’t want 2 comments from the same post to have the same  
title. How is the easiest way to test that in my Comment model ?

---

<div class="post-metadata">

**Author:** ![DavidPaquet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@DavidPaquet](https://www.ruby-forum.com/u/DavidPaquet)\
**Post date:** [June 22, 2008, 12:46am UTC](https://www.ruby-forum.com/t/newbie-easiest-way-to-validate-uniqueness-of-2-fields-that-are-related/141096/2 "2008-06-22T00:46:17Z")

</div>

On Jun 21, 8:27 pm, DavidPaquet [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello everyone
> 
> Just to be sure, what is the easiest way to validate that a  
> combination of 2 fields is unique in my model ?

That’s what the :scope option on validates\_uniqueness is for.

Fred
