# Help with validates \_uniqueness\_of for a has\_many table

**URL:** <https://www.ruby-forum.com/t/help-with-validates-uniqueness-of-for-a-has-many-table/140201>\
**Category:** Rails\
**Created:** [June 11, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/help-with-validates-uniqueness-of-for-a-has-many-table/140201 "2008-06-11T21:00:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 11, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/help-with-validates-uniqueness-of-for-a-has-many-table/140201/1 "2008-06-11T21:00:27Z")

</div>

i have a list of COMPOUNDS (combinations) that have any number of  
COMPOUND\_MATERIALS of which both are ITEMS. (this is for a video game  
item database)

i want to validate that the compounds are unique by validating it’s  
combination of compound\_materials does not already exist (a compound can  
have multiple combinations of materials)

my tables:

ITEMS  
id  
name  
…

COMPOUNDS (has many compound materials)  
id  
item\_id

COMPOUND\_MATERIALS  
id  
compound\_id  
item\_id

so how would i use this in my model?

validates\_uniqueness\_of :item\_id, scope =\>  
[‘compound\_materials.item\_id’, …

something like that but i’m confused because a compound can have any  
number of compound\_materials so I’m not sure how to write that out.

thanks.
