Permalink
Browse files

Merge branch 'master' of github.com:emberjs/data

  • Loading branch information...
2 parents 8f6908f + 101d692 commit 5eb88f202a35e8a429d5a0d85db4d29875b12532 tomhuda committed Oct 30, 2012
View
5 packages/ember-data/lib/serializers/rest_serializer.js
@@ -10,9 +10,8 @@ DS.RESTSerializer = DS.Serializer.create({
},
addBelongsTo: function(hash, record, key, relationship) {
- var hashKey = this._keyForBelongsTo(record.constructor, key),
- id = get(record, key+'.id');
+ var id = get(record, relationship.key+'.id');
- if (!Ember.none(id)) { hash[hashKey] = id; }
+ if (!Ember.none(id)) { hash[key] = id; }
}
});
View
6 packages/ember-data/lib/system/serializer.js
@@ -251,11 +251,15 @@ DS.Serializer = Ember.Object.extend({
addRelationships: function(hash, record) {
record.eachAssociation(function(name, relationship) {
- var key = this._keyForAttributeName(record.constructor, name);
+ var key;
if (relationship.kind === 'belongsTo') {
+ key = this._keyForBelongsTo(record.constructor, name);
+
this.addBelongsTo(hash, record, key, relationship);
} else if (relationship.kind === 'hasMany') {
+ key = this._keyForHasMany(record.constructor, name);
+
this.addHasMany(hash, record, key, relationship);
}
}, this);
View
26 packages/ember-data/tests/unit/rest_adapter_test.js
@@ -950,3 +950,29 @@ test("data loaded from the server is converted from underscores to camelcase", f
equal(person.get('name'), "Tom", "precond - data was materialized");
equal(person.get('lastName'), "Dale", "the attribute name was camelized");
});
+
+test("When a record with a belongsTo is saved the foreign key should be sent.", function () {
+ var PersonType = DS.Model.extend({
+ title: DS.attr("string"),
+ people: DS.hasMany(Person)
+ });
+
+ Person.reopen({
+ personType: DS.belongsTo(PersonType)
+ });
+
+ store.load(PersonType, {id: 1, title: "Developer"});
+ var personType = store.find(PersonType, 1);
+
+ // FIXME this mass-assignment of a belongs to is broken. It must be set separately.
+ // var person = store.createRecord(Person, {name: 'Sam Woodard', personType: personType});
+ var person = store.createRecord(Person, {name: 'Sam Woodard'});
+ person.set('personType', personType);
+
+ store.commit();
+
+ expectUrl('/people');
+ expectType("POST");
+ expectData({ person: { name: "Sam Woodard", person_type_id: "1" } });
+ ajaxHash.success({ person: { name: 'Sam Woodard', person_type_id: 1}});
+});

0 comments on commit 5eb88f2

Please sign in to comment.