Removing duplicates from HABTM

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!