# Check if value has already been used

**URL:** <https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659>\
**Category:** Ruby\
**Created:** [December 19, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659 "2007-12-19T16:09:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [December 19, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659/1 "2007-12-19T16:09:07Z")

</div>

Hi,

Im currently working on an issue im having with a ruby method within my  
rails project.

i have this method:

```
def saveAssetTags(asset, taglist)
 #add new tags

 tagList = taglist.split(/\s*\,\s*/)
 #drop all current tags
 asset.tags.clear
 for metadata in tagList
   tag = Tag.find_by_Tag(metadata)
   unless tag
     tag = Tag.new
     tag.Tag = metadata
     tag.save
   end
   asset.tags << tag
 end

```

end

its taking in a list of tags from the client seperated by comma’s and  
putting them into an array before saving them in my database. this all  
works fine unless a user enters the same tag twice in which case i get a  
duplication error in the db.

whats the best way to check the array for repeating attributes or to  
check as i iterate through the loop?

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [December 19, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659/2 "2007-12-19T16:25:01Z")

</div>

Alle mercoledÃ¬ 19 dicembre 2007, Chris G. ha scritto:

> ```
> tagList = taglist.split(/\s*\,\s*/)
> end
> 
> ```
> 
> Cheers,
> 
> Chris

I can think of two approaches. If you don’t want to take any special  
action if  
there are duplicates tags (such as report an error), you can remove the  
duplicates from the array using the Array#uniq method:

tagList = taglist.split(/\s\*,\s\*/).uniq

The other possibility is to store the found tags in a hash or array.  
Something  
like:

found\_tags = {}  
for metadata in tagList  
if found\_tags.has\_key? metadata  
#do what you need to do for a duplicate tag.  
else  
found\_tags[metadata] = true  
#do what you need to do for a non-duplicate tag  
end  
end

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [December 19, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659/3 "2007-12-19T16:25:20Z")

</div>

not to worry, found the answer myself by thinking about it.

just added .uniq to tagList :0

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [December 19, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/check-if-value-has-already-been-used/123659/4 "2007-12-19T16:27:45Z")

</div>

thanks stefano 🙂 good to know I was on the right track!
