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 ['workgr开发者_StackOverflowoupInstance':workgroupInstance, 'users':usersWithoutThisOne]
}
Results in this being displayed in flash.message
[boogie, toogie, choogie, cookie]removed boogie, new list is [boogie, toogie, choogie, cookie]
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()
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.
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]
精彩评论