# Removing duplicates from HABTM

**URL:** <https://www.ruby-forum.com/t/removing-duplicates-from-habtm/157372>\
**Category:** Rails\
**Created:** [January 10, 2009, 3:41am UTC](https://www.ruby-forum.com/t/removing-duplicates-from-habtm/157372 "2009-01-10T03:41:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [January 10, 2009, 3:41am UTC](https://www.ruby-forum.com/t/removing-duplicates-from-habtm/157372/1 "2009-01-10T03:41:48Z")

</div>

Hey, folks.

I have articles, which have and belong to many tags. For a given  
article, I need to produce a list of tags and associated articles, but  
make sure that an associated article doesn’t appear under more than  
one tag. Also need to limit the list to 3 articles for each tag, and  
make sure the given article doesn’t appear in the list.

I’ve started it like so, but it’s ugly and still contains duplicate  
articles, and I’m drifting into array/hash insanity.

for tag in article.tags  
puts tag.name  
for relatedArticle in tag.articles  
puts relatedArticle.title  
end  
end

Any suggestions appreciated!
