I am using Mongodb (Rails 3 + Mongoid) and Angular JS.
In my db, I have a collection users
which holds an array of objects addresses
. I am trying to update the fields on an address in the array, but when I send the update request (using Angular's resourceProvider
), all the _id
that Angular sends to my server is "{}
" (i.e. empty), so I end up with duplication instead of modification.
$scope.user.addresses holds non-blank ids and looks something like this:
[{_id:{$oid:"123431413243"}, text:"123 fake", cat:1},{_id:{$oid:"789789078907890}, text:"789 test", cat:7},...]
The PUT request body holds empty ids and looks something like this:
{"_id":{}, "user":{"addresses_attributes":[{"_id":{}, "text":"123 fake", "cat":"1"},{"_id":{}, "text":"789 test", "cat":"7"},...]}}
Angular JS code
myApp.factory('Users', ['$resource', function ($resource) {
return $resource( '/users/:id.json', {id:0}, {update: {method:'PUT', _method:'PUT'}} );
}]);
myApp.controller('UsersCtrl', function ($scope, Users) {
$scope.save = function () {
$scope.user.$update({}, {user:$scope.user});
};
});
Do you have any idea why this is and what I can do about it?