Anonymous Human
Anonymous Human

Reputation: 1948

Gorm finder with dynamic argument (part 2)?

I have the following domain classes:

class AccountRecord {
     List owners // owner of this account
}

and

class IndividualRecord {
     String uniqueId
     String secondaryId
     List accounts // accounts owned by this individual
}

The owners List in the AccountRecord class stores all the owners of that account by storing maps that contain pairs of each owner's uniqueId and secondaryId. So, an owners list might look like:

[
[uniqueId: 1, secondaryId 2] // owner 1 
[uniqueId: 2, secondaryId 3] // owner 2
]

Let's say that I want to use GORM to find all AccountRecord's whose owners list contains a map such that the map contains a given IndividualRecord's uniqueId and secondaryId. Basically, given an individual, find all accounts that are owned by the individual. Currently I am doing this but it isn't working(its also returning accounts not owned by the individual for some reason):

def accountsOwnedByUser = AccountRecord.where {[uniqueId: someOwner.uniqueId, secondaryId: someOwner.secondaryId] in owners}.list()

Any suggestions?

Upvotes: 0

Views: 48

Answers (1)

Mario David
Mario David

Reputation: 1615

If there is no paticular reason why you don't use the standard gorm M:N Relationship, you could just implement it like this:

class AccountRecord {
  static hasMany = [owners: IndividualRecord]
}

class IndividualRecord {

  String uniqueId
  String secundaryId

  static belongsTo = AccountRecord
  static hasMany = [accounts: AccountRecord]
}

Basically, given an individual, find all accounts that are owned by the individual

this could then be easily achieved by using the association: myIndividual.accounts.

Upvotes: 1

Related Questions