# :scope (validates\_uniqueness) question

**URL:** <https://www.ruby-forum.com/t/scope-validates-uniqueness-question/133661>\
**Category:** Rails\
**Created:** [April 2, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/scope-validates-uniqueness-question/133661 "2008-04-02T12:07:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [April 2, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/scope-validates-uniqueness-question/133661/1 "2008-04-02T12:07:30Z")

</div>

Consider the following table:  
±—±-------±-------±----------+  
| id | seq\_no | request| response |  
±—±-------±-------±----------+  
| 42 | 1 | req1 | 0106 |  
| 43 | 2 | req2 | 2343 |  
| 44 | 3 | test | OK |  
±—±-------±-------±----------+

I in the model that wraps this table, i have defined a constraint that  
validates the uniqueness of the response for each request type. I.e.  
validates\_uniqueness\_of :response, :scope =\> :request, :if =\> :response?

However, I not want to modify this constraint so that the uniqueness is  
only validated for requests re1 and req2. i.e. I want to be able to  
create the following table.  
±—±-------±-------±----------+  
| id | seq\_no | request| response |  
±—±-------±-------±----------+  
| 42 | 1 | req1 | 0106 |  
| 43 | 2 | req2 | 2343 |  
| 44 | 3 | test | OK |  
| 45 | 4 | test | OK |  
±—±-------±-------±----------+

What should my validates\_uniqueness\_of statement look like?

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [April 2, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/scope-validates-uniqueness-question/133661/2 "2008-04-02T13:07:23Z")

</div>

Tim C. wrote:

> Consider the following table:  
> ±—±-------±-------±----------+  
> | id | seq\_no | request| response |  
> ±—±-------±-------±----------+  
> | 42 | 1 | req1 | 0106 |  
> | 43 | 2 | req2 | 2343 |  
> | 44 | 3 | test | OK |  
> ±—±-------±-------±----------+
> 
> I in the model that wraps this table, i have defined a constraint that  
> validates the uniqueness of the response for each request type. I.e.  
> validates\_uniqueness\_of :response, :scope =\> :request, :if =\> :response?
> 
> However, I not want to modify this constraint so that the uniqueness is  
> only validated for requests re1 and req2. i.e. I want to be able to  
> create the following table.  
> ±—±-------±-------±----------+  
> | id | seq\_no | request| response |  
> ±—±-------±-------±----------+  
> | 42 | 1 | req1 | 0106 |  
> | 43 | 2 | req2 | 2343 |  
> | 44 | 3 | test | OK |  
> | 45 | 4 | test | OK |  
> ±—±-------±-------±----------+
> 
> What should my validates\_uniqueness\_of statement look like?

Hi Tim,  
May be this will help

validates\_uniqueness\_of :response, :scope =\> :request, :if =\>  
ModelName.new{|model\_names| model\_names.request != “test” }
