Have you ever needed to retrieve the intersection of arrays in an array. Well, I did.

Let's say that you have a has and belongs to many (habtm) association between articles and tags, (I.e., a tag habtm articles and an article habtm tags). This means that tag.articles will return a list of articles that have that tag and article.tags will return the tags of the article. So if a user wants to search your blog for all articles that have 'tag1' and 'tag2', you would return all common articles.

Picture a search page with a list of check boxes, one for each tag that you have. The user can click as many tags as they want and then it is your job to find the common articles between them. The search POST contains the ids of the selected tags, like params[:tag_ids] = ["1", "3", "15"] or something. Let's go with this example.

1) The first step is to find those tags:

1

>> tags = Tag.find(params[:tag_ids])

2) Next, we want to get the articles associated with each tag, we will work with the ids: