Question

I have a domain named thana where I put all the thanaName. But I don't want to save any duplicate name. There may be a lot of way for doing this but which will be much smarter I don't know. Can anyone please help me on this. any example or source code will do the work perfectly. thanks in advance for watching the question.

Was it helpful?

Solution

This sounds like the prefect use case for the unique constraint.

class MyDomain {
  String name
  OtherDomain related

  static constraints = {
    name unique: ['related'] // each instance must have a unique name per related
  }
}

Edit

Updated based on question in comment. The above will ensure that name is unique for each related. So, for example if MyDomain A has a related instance id of 1 and a name of "Test" no ohter instance of MyDomain with the same related instance can have the name of "Test". However, MyDomain B which has a rleated instance id of 2 can have a name of "Test" since the unique is per "related" in the above example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top