Article {
String categoryName
static hasMany = [
tags: Tag
]
}
Tag {
String name
}
Now I want to find the list of all related articles. Related meaning, all articles that have the same category name as myArticle or any of the same tags as myArtcle.
With only matching categoryName, here is how I would get the relatedArticles using closures.
def relatedArticles = Article.list().find {it.categoryName == myArticle.categoryName }
Anyone wants to give a shot to find all articles by CategoryName or Tag Name (in a groovy way)?
Any solutions using Criteria or custom queries is appreciated too.
This would work:
However, if you have a reasonably large number of articles and only a small number of them are expected to match it would be horribly inefficient, as this will load all articles then iterate through them all looking for matches.
A better way would be to simply write a query that only loads matching articles (instead of calling
Article.list())