Reputation: 2516
I have the following domain structure:
class Emloyee {
static hasMany = [users: User]
.....
}
And I want to code a method that
my code looks like:
def deleteUser(User userInstance) {
def employee = Employee.find { users.contains(userInstance) }
employee .removeFromUsers(userInstance)
employee .save(flush: true, failOnError: true)
userInstance.delete(flush: true, failOnError: true)
}
and this code gives me an exception:
No signature of method: grails.gorm.DetachedCriteria.contains() is applicable for argument types: (User)
What I've done wrong? Thank you!
Upvotes: 0
Views: 100
Reputation: 35
Does a user always contain to one employee?
Then you can use
static belongsTo = [employee: Employee]
in your User domain class. In this case you don't need to manually delete the user from the employee domain. GORM delete it when you call userInstance.delete(...)
If you don't want to use the belongsTo than you can delete the user this way:
def deleteUser(User userInstance) {
def c = Employee.createCriteria()
def result = c.get {
users {
idEq(userInstance.id)
}
}
result.removeFromUsers(userInstance)
userInstance.delete(flush: true, failOnError: true)
}
Hope this helps. Sven
Upvotes: 1