Search code examples
grailsgrails-orm

How to check some data in a join table by userId in grails?


first of all I want to tell that I'm quite new in grails and maybe could be that this question seems easy but I'm spending some hours in this.

I'm trying to check for a user that his expires data has expired

Date currentDate = new Date(System.currentTimeMillis())
        def authRequest = Authentication.findAllByExpiresLessThan(currentDate)

With the method of up, I can get all the users in a list that his expires data has expired. Notice that I'm getting a list

My problem comes now, because if his expires data has expired, I need to change the status of the user to REQUESTED => ACTIVE. And this is that I don't know how to check in grails starting with the data that I request from authRequest, because I can see in a debug mode that I get all the columns that have the object, but not sure how can I join with the other table

This is table/domain user:
name|surname|status|user_id
bob   len    REQUESTED asdfwerrg
Marc  Marc   ACTIVE    jryut6u5u

This is table/domain authentication
expires             |  user_id
2016-04-17 11:38:50 | asdfwerrg
2016-05-10 11:38:50 | asdfwerrg

Hope the explanation is ok and thanks in advance


Solution

  • You can use

    def user = User.findAllByUserIdInList(authRequest.userId.toList())
    

    then

    user.each{
         it.status = "ACTIVE"
         it.save(flush:true)
    }
    

    Please see http://grails.github.io/grails-doc/2.1.0/ref/Domain%20Classes/findAllBy.html for more details.

    Thanks