# Validates\_uniqueness\_of on has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-on-has-and-belongs-to-many/73633>\
**Category:** Rails\
**Created:** [December 20, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-has-and-belongs-to-many/73633 "2005-12-20T20:17:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [December 20, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-has-and-belongs-to-many/73633/1 "2005-12-20T20:17:14Z")

</div>

I am trying to create a model that allows only unique assets to be  
added to a category. However, they could be added again to a different  
category. Finally, an asset can be shared across many categories. So  
my question is how do you check the uniqueness across a join table?

This does not compile…

has\_and\_belongs\_to\_many :categories, :foreign\_key =\> ‘asset\_id’  
validates\_uniqueness\_of :data, :scope =\> ‘asset\_categories.category\_id’

Thanks,  
Mark

–
