Ant's
Ant's

Reputation: 13811

How to delete a relationship in this case?

I have User class:

class User {
       static hasMany = [ project: Project ]
}

and Project:

class Project {
     static hasMany = [ users : User ]
     static belongsTo = User
}

Anywhere in my code I can do this :

user.project

will return something like :

[project name]

I need a way to delete this relationship for any User. How can I do that? I can find a user project using dynamic finders, criteria query so on, but how do I remove that relationship from a user instance?

Very new to Grails.

Thanks in advance.

Upvotes: 0

Views: 56

Answers (1)

ataylor
ataylor

Reputation: 66059

Use removeFrom. Example:

user.removeFromProject(project)

In many-to-many relationships, you should manage the associations from the owning side: the one that is the target of the belongsTo.

Note that it would probably be more clear to name the Project association "projects" instead of "project", since it's a many-to-many relationship.

Upvotes: 1

Related Questions