I have a domain class
Post {
hasMany [comments : Comment]
}
Comment {
belongsTo [post: Post]
User user
}
I would like to search all the posts that contain any comment from a specific User.
Something like
def posts = Post.findAll
{(
//condition1 ||
// condition 2 ||
comments.containsAny(Comment.findByUser(User.get(params.userId)))
//if the post contains any comment from this user, get it
)}
Any idea how i can do this ?
Thank you
This criteria query should do it
def user = User.get(params.userId)
def posts = Post.createCriteria().listDistinct {
comments {
eq 'user', user
}
}
listDistinct
ensures that if a user comments on a post several times, the post is only retrieved once.