Quantcast

Why can't i delete?. What is wrong with me??

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Why can't i delete?. What is wrong with me??

Fendy82
This post has NOT been accepted by the mailing list yet.
Greetings from Malaysia!!.
Not sure if this problem has already been posted. If it does, then i apologize.

The Code:
class User {
   
    String userId
    String password
    User follower    

    static constraints = {
        userId(blank:false, size:3..12)
        password(blank:false, size:6..12, validator:{passwd, user -> return passwd != user.userId})
    }
   
    String toString(){
        return userId
    }
}


The Scenario:
I created a class User that has a one-to-one relationship with it self. Then i created User A and User B,  assigned User B as a follower to User A.

The Problem:
Why is it i can't delete User B without having to delete user A first???
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why can't i delete?. What is wrong with me??

gatherer
Because User A keeps a reference to User B. If you just deleted B, A's reference would became invalid, pointing to nowhere. Then, if someone tried to access A's follower, there would be an exception.

You can do two things in this case, you can cascade the delete, that is, delete A before B (automatically with a hasMany/belongsTo, from beforeDelete or manually from the controller), or you can set A's follower to null (have to add the nullable constraint first) or point it to another valid User object before deleting.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why can't i delete?. What is wrong with me??

Fendy82
This post has NOT been accepted by the mailing list yet.
Thanks for the explanation. I'm still new at programming but it really does make sense.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why can't i delete?. What is wrong with me??

Fendy82
This post has NOT been accepted by the mailing list yet.
In reply to this post by gatherer
OK, based on what you have described, i tried using filters;

The Code:
class BeforeDeleteFilters {

    def filters = {
        all(controller:'user', action:'delete') {
            before = {
               
                def userInstance = User.get(params.id)
               
                if(userInstance.follower){
                   
                    def parentUser = User.findWhere('follower.id':params.id.toLong())
                   
                    if(parentUser){
                        parentUser.properties = ['follower.id':null]
                        parentUser.save()
                    }else{
                        return false
                    }
                }
            }
            after = {
               
            }
            afterView = {
               
            }
        }
    }    
}

but it still not working, so i tried doing it manually via controllers

The Code:
def beforeDelete = {
       
        def parentUser = User.findWhere('follower.id':params.id.toLong())
       
        if(parentUser){
            parentUser.properties = ['follower.id':null]
            parentUser.save()
           
            redirect(action:'delete', id:params.id)
        }else{
            redirect(action:'list')
        }
    }

and still not working. Am i missing something??
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why can't i delete?. What is wrong with me??

gatherer
In reply to this post by gatherer
I don't see why you need to use filters. You can put your "reference cleanup" code in the beforeDelete closure in your class definition (not in the controller) or in the controller's delete action. Again, if you put your cleanup code in the controller delete action, don't use filters.

def delete {
    // Cleanup code
    // Delete code
}

If I were you I would learn to use some grails/groovy constructs which result in much more readable code. For instance:

User.findByFollower(followerInstance) // You don't need to mess with ids
parentUser.follower = null // You can access the follower property directly, no need to use the .properties thing
Loading...