# Validates\_uniqueness\_of - how to use multiple values for sco

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-how-to-use-multiple-values-for-sco/65783>\
**Category:** Rails\
**Created:** [February 11, 2006, 8:03am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-to-use-multiple-values-for-sco/65783 "2006-02-11T08:03:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_Rose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7cd45c/32.png) [@Iain\_Rose](https://www.ruby-forum.com/u/Iain_Rose)\
**Post date:** [February 11, 2006, 8:03am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-to-use-multiple-values-for-sco/65783/1 "2006-02-11T08:03:39Z")

</div>

Hi

I’m trying to create a basic test management tool to organise the manual  
testing I have to do. I’m working on the results recording module and  
have a  
problem.

Basically, I want each test result to belong to a test case, a test  
environment and a build.

I want to make sure there’s no duplication of results with these 3  
properties. If someone tries to enter a new test result for a test thats  
already been run on a certain environment with a certain build I want  
the  
user to be prompted to update the existing result rather than create a  
new  
one.

In the result model, I know I can use …

validate\_uniqueness\_of :test\_id, :scope =\> “xxxxx”

… but I need it to be unique for 2 fields (environment and build)

Is there any way to pass 2 values into the scope attribute?

If not can anyone suggest how to update the validate\_uniqueness\_of  
method to  
do this?

thanks

Iain

---

<div class="post-metadata">

**Author:** ![Iain\_Rose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7cd45c/32.png) [@Iain\_Rose](https://www.ruby-forum.com/u/Iain_Rose)\
**Post date:** [February 11, 2006, 8:34am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-to-use-multiple-values-for-sco/65783/2 "2006-02-11T08:34:13Z")

</div>

You can do this only using edge(svn) version of Rails. You should pass  
an array of scope fields.

Kent

---

<div class="post-metadata">

**Author:** ![Iain\_Rose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7cd45c/32.png) [@Iain\_Rose](https://www.ruby-forum.com/u/Iain_Rose)\
**Post date:** [February 11, 2006, 8:44am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-to-use-multiple-values-for-sco/65783/3 "2006-02-11T08:44:20Z")

</div>

## I heard of an upcoming change to validates\_uniqueness\_of that will allow for multiple validation, but until then, this should work:

# Make sure each (id, environment, build) tuple is unique for model

## TestCase validates\_each :test\_id, :on =\> :create do |record, test, value| if TestCase.find\_by\_id\_and\_environment\_and\_build(record.attributes[‘id’], record.attributes[‘environment’], record.attributes[‘build’]) record.errors.add test, “This (id,environment,build) tuble has already been added” end end

– Jeff
