Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #447 from shwoodard/master-with-serializer-associa…

…tion-name

Add test for #418
  • Loading branch information...
commit 101d692117cc9dc9a89e97e5f1602b9e62b8278b 2 parents 77d3153 + c6fec5c
Peter Wagenet authored October 30, 2012
5  packages/ember-data/lib/serializers/rest_serializer.js
@@ -10,9 +10,8 @@ DS.RESTSerializer = DS.Serializer.create({
10 10
   },
11 11
 
12 12
   addBelongsTo: function(hash, record, key, relationship) {
13  
-    var hashKey = this._keyForBelongsTo(record.constructor, key),
14  
-        id = get(record, key+'.id');
  13
+    var id = get(record, relationship.key+'.id');
15 14
 
16  
-    if (!Ember.none(id)) { hash[hashKey] = id; }
  15
+    if (!Ember.none(id)) { hash[key] = id; }
17 16
   }
18 17
 });
6  packages/ember-data/lib/system/serializer.js
@@ -251,11 +251,15 @@ DS.Serializer = Ember.Object.extend({
251 251
 
252 252
   addRelationships: function(hash, record) {
253 253
     record.eachAssociation(function(name, relationship) {
254  
-      var key = this._keyForAttributeName(record.constructor, name);
  254
+      var key;
255 255
 
256 256
       if (relationship.kind === 'belongsTo') {
  257
+        key = this._keyForBelongsTo(record.constructor, name);
  258
+
257 259
         this.addBelongsTo(hash, record, key, relationship);
258 260
       } else if (relationship.kind === 'hasMany') {
  261
+        key = this._keyForHasMany(record.constructor, name);
  262
+
259 263
         this.addHasMany(hash, record, key, relationship);
260 264
       }
261 265
     }, this);
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
950 950
   equal(person.get('name'), "Tom", "precond - data was materialized");
951 951
   equal(person.get('lastName'), "Dale", "the attribute name was camelized");
952 952
 });
  953
+
  954
+test("When a record with a belongsTo is saved the foreign key should be sent.", function () {
  955
+  var PersonType = DS.Model.extend({
  956
+    title: DS.attr("string"),
  957
+    people: DS.hasMany(Person)
  958
+  });
  959
+
  960
+  Person.reopen({
  961
+    personType: DS.belongsTo(PersonType)
  962
+  });
  963
+
  964
+  store.load(PersonType, {id: 1, title: "Developer"});
  965
+  var personType = store.find(PersonType, 1);
  966
+
  967
+  // FIXME this mass-assignment of a belongs to is broken.  It must be set separately.
  968
+  // var person = store.createRecord(Person, {name: 'Sam Woodard', personType: personType});
  969
+  var person = store.createRecord(Person, {name: 'Sam Woodard'});
  970
+  person.set('personType', personType);
  971
+
  972
+  store.commit();
  973
+
  974
+  expectUrl('/people');
  975
+  expectType("POST");
  976
+  expectData({ person: { name: "Sam Woodard", person_type_id: "1" } });
  977
+  ajaxHash.success({ person: { name: 'Sam Woodard', person_type_id: 1}});
  978
+});

0 notes on commit 101d692

Please sign in to comment.
Something went wrong with that request. Please try again.