In requests to my application i often get ids of the objects, which should be associated. However I have to perform a check to see if they are.
Example scenario:
Class A and B are associated:
A {
static hasMany = [bs: B]
}
In my request I will get aid and bid.
What I usually do is:
def a = A.get(aid)
def b = a.bs.find {it.id == bid}
What would be a better way to make this check? From performance point of view?
Thanks
If B has a belongsTo = [ a : A ] defined in it, then you can do this:
This won’t do an iteration over all the sets elements like your code. Essentially, it’s the same as erturne’s solution, but this actually loads the object.