# Validates\_uniqueness\_of :scope =\> association

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-scope-association/175581>\
**Category:** Rails\
**Created:** [September 15, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-scope-association/175581 "2009-09-15T23:57:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [September 15, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-scope-association/175581/1 "2009-09-15T23:57:10Z")

</div>

(How?) Is it possible to use validates\_uniqueness\_of with a scope that  
is a  
belongs\_to association?

This is what I would like to do:

class Lot \< ActiveRecord::Base  
belongs\_to :part, :include =\> true  
validates\_uniqueness\_of :identifier, :scope =\> :part  
end

…but (not surprisingly) when I run my unit test, I get:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:  
lots.part

Basically, I want to ensure that identifiers are unique for a given  
part.

I think I’m going to have to write my own validation here, but I thought  
I  
would ask the question first.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [September 15, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-scope-association/175581/2 "2009-09-15T23:59:31Z")

</div>

On Sep 15, 9:44 pm, Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …but (not surprisingly) when I run my unit test, I get:
> 
> ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:  
> lots.part
> 
> Basically, I want to ensure that identifiers are unique for a given part.
> 
> I think I’m going to have to write my own validation here, but I thought I  
> would ask the question first.

Well validates uniqueness of wants scope to be column, so give it one  
(part\_id) !

Fred

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [September 16, 2009, 1:57am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-scope-association/175581/3 "2009-09-16T01:57:11Z")

</div>

On Tue, Sep 15, 2009 at 5:57 PM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> > Basically, I want to ensure that identifiers are unique for a given part.
> 
> Well validates uniqueness of wants scope to be column, so give it one  
> (part\_id) !
> 
> Fred

Oh… duh!  
(sheepishly creeps back into his cave with very little light)

Thanks Fred.

And thanks for answering such blatantly simple questions.

–wpd
