我正在尝试像推特一样关注用户。当我尝试保存当前登录的会话用户的用户时,我收到异常。
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'
用户域:
class User {
String email_id;
String password;
Profile profile
static hasMany = [ posts:Post, following: User ]
}
控制器操作
def follow(){
def followUser = User.get(params.id)
def user = User.get(session.user)
user.following = followUser
user.save()
}
答案 0 :(得分:4)
向集合添加实例时,必须对相应的集合使用addTo
*方法。 documentation更详细地解释了这一点。
您的行动应如下所示:
def follow(){
def followUser = User.get(params.id)
def user = User.get(session.user)
user.addToFollowing(followUser)
user.save()
}