I've been trying to add to an array (or what ruby is saying is an array), but keep getting an error from mongo which says
Cannot apply $addToSet modifier to non-array
when I try to run
User.collection.update({'id'=> current.id},{'$addToSet'=>{ 'following' => current.id}}) User.collection.update({'id'=> user.id},{'$addToSet'=>{ 'following' => user.id}})
or the mongomapper version
User.push_uniq(current.id, :following => user.id) User.push_uniq(user.id, :followers => current.id)
When I output
<%= debug @user.following.kind_of? Array %>
returns true
However, when running
db.users.find()
directly agains mongo, I get
{ "_id" : ObjectId("4c4a196f15a79004e0000007"), "email" : "[email protected]", "foll owers" : null, "following" : null, "password_hash" : "98f2188de42bce1554d08fbc81 d5c99a2c234933", "password_salt" : "25d80a83cfe3d126cdbe9fec2b731ab9ea57c3b8", " username" : "test" }
I would have expected following and followers to be [], not null.
When I output debug @user.followers, rails shows --- []
My model to create the user is
key :username, :type => String key :email, :type => String key :password_hash, :type => String key :password_salt, :type => String key :followers, :type => Array key :following, :type => Array
The error leads me to believe that the user.followers is being found, but can't be updated. When I change
User.push_uniq(current.id, :testing => user.id)
I don't get an error, so I think i have that part right. Any suggestions?