I am trying to make a follow user like in twitter. I'm getting an exception when I try to save the user of the currently logged in session user.
Error 500: Internal Server Error
URI - /blog-dwit/user/follow/3
Class - org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message - Cannot cast object 'blog.dwit.User : 3' with class 'blog.dwit.User' to class 'java.util.Set'
User domain:
class User {
String email_id;
String password;
Profile profile
static hasMany = [ posts:Post, following: User ]
}
Controller action
def follow(){
def followUser = User.get(params.id)
def user = User.get(session.user)
user.following = followUser
user.save()
}
When adding instances to collections you have to use the addTo
* method for the corresponding collection. The documentation explains this in further detail.
Your action should look like this:
def follow(){
def followUser = User.get(params.id)
def user = User.get(session.user)
user.addToFollowing(followUser)
user.save()
}