Groovy on Grails list - not working?

Removing an element from a list isn't working, which doesn't make any sense. Am I missing some special semantics peculiar to dealing with a Grails domain object list?

In controller:

def userCreate = {
    def workgroupInstance = new Workgroup()
    workgroupInstance.manager = authUserDomain
    flash.message = User.list().toString()
    def usersWithoutThisOne = User.list() - authUserDomain
    flash.message = flash.message + "removed ${authUserDomain}, new list is ${usersWithoutThisOne}"
    return ['workgroupInstance':workgroupInstance, 'users':usersWithoutThisOne]
}

Results in this being displayed in flash.message

[boogie, toogie, choogie, cookie]removed boogie, new list is [boogie, toogie, choogie, cookie]

Upvotes: 1

Views: 2913

Answers (3)

Blacktiger
Blacktiger

Reputation: 1275

If you intend to remove the user from the workgroup permanently, then you need to use the grails removeFrom function to get rid of classes that are stored in a has many association.

Upvotes: 2

leebutts
leebutts

Reputation: 4882

Where does authUserDomain come from?

If you haven't implemented a customn .equals() on User (based on username or someother unique identified) then it may not be the same object that is returned via User.list(). An element will only be removed if it matches an existing object using .equals()

Upvotes: 4

Dirk
Dirk

Reputation: 31053

I am not sure about this one. Don't have a Groovy interpreter available right now. But IIRC and as this article suggests, the - on lists expects to operate on two lists, i.e.,

list - other

is actually more like

list.removeAll(other)

(in Java terms), not the intended

list.remove(other)

You might try

modifiedList = originalList - [elementToRemove]

Upvotes: 0

Related Questions