问题
I have a list of events like so:
events = Events.all
Events
have a many-to-many relationship with Users
via class UserEvents
modeled after the approach from spring-security-plugin:
I would like to find out whether a User
is attending an Event
and I can do that by running this:
UserEvent.get(currentUserId, eventId)
Question
How can I do this over all the elements of my list events
so that in my view layer I can easily find out whether currentUserId
is going to the event?
回答1:
You can query for all of the associated User
s for an Event
like this:
def user = ...
def events = UserEvent.findAllByUser(user).event
This is a fairly efficient query since it executes SQL similar to
select * from user_event where user_id=?
and then loads each Event
. This is N+1 though since it loads each Event individually, so you can do it more efficiently with this:
def eventIds = UserEvent.findAllByUser(user).eventId
def events = Event.getAll(eventIds)
The first line just gets the event ids using the same SQL as above, then the second line runs SQL like
select * from event where id in (?, ?, ?, ...)
回答2:
If you only need a subset of your events which the user attends, then go with what Burt Beckwith sugeested.
But, if you need the whole set of events and just add an attribute you should use metaClass.
event.metaClass.userAttends= ...
you can see explanations about metaClass here
Specifically in your situation i would get all ids as Burt Beckwith said and the check for each event if its id is in the list:
def eventIds = UserEvent.findAllByUser(user).eventId
expandedEvents = events.collect {event->
event.metaClass.usetAttends = eventIds.contains(event.id)
return event}
回答3:
You could use grep() to filter the list of events by whether UserEvent.get()
returns an item.
events.grep { UserEvent.get(currentUserId, it.id) }
来源:https://stackoverflow.com/questions/15141202/how-to-iterate-over-a-list-in-groovy-and-add-property-for-each-object