Note that there are some explanatory texts on larger screens.

plurals
  1. POember-data belongsTo not working
    primarykey
    data
    text
    <p>I have the following model:</p> <p>and when I call this.store.find('history');</p> <p>a call to http:://www.example.com/api/histories/ is made and this JSON response is returned:</p> <pre><code>{ "tracks":[ { "id":83, "title":"Untitled", "length":148, "artist_ids":[ ], "album_ids":[ ] }, { "id":85, "title":"You want it", "length":262, "artist_ids":[ ], "album_ids":[ ] }, { "id":81, "title":"Untitled", "length":133, "artist_ids":[ ], "album_ids":[ ] }, { "id":78, "title":"Untitled", "length":345, "artist_ids":[ ], "album_ids":[ ] }, { "id":80, "title":"Untitled", "length":225, "artist_ids":[ ], "album_ids":[ ] }, { "id":73, "title":"Untitled", "length":366, "artist_ids":[ ], "album_ids":[ ] }, { "id":77, "title":"Untitled", "length":161, "artist_ids":[ ], "album_ids":[ ] }, { "id":82, "title":"Untitled", "length":384, "artist_ids":[ ], "album_ids":[ ] }, { "id":76, "title":"Untitled", "length":245, "artist_ids":[ ], "album_ids":[ ] }, { "id":79, "title":"Untitled", "length":479, "artist_ids":[ ], "album_ids":[ ] }, { "id":75, "title":"Untitled", "length":328, "artist_ids":[ ], "album_ids":[ ] }, { "id":84, "title":"Untitled", "length":259, "artist_ids":[ ], "album_ids":[ ] }, { "id":74, "title":"Untitled", "length":329, "artist_ids":[ ], "album_ids":[ ] } ], "albums":[ ], "artists":[ ], "histories":[ { "id":1382220844, "time_played":"2013-10-20 00:14:04", "user_id":null, "track_id":83 }, { "id":1382220581, "time_played":"2013-10-20 00:09:41", "user_id":null, "track_id":85 }, { "id":1382220449, "time_played":"2013-10-20 00:07:29", "user_id":null, "track_id":81 }, { "id":1382220103, "time_played":"2013-10-20 00:01:43", "user_id":null, "track_id":78 }, { "id":1382219877, "time_played":"2013-10-19 23:57:57", "user_id":null, "track_id":80 }, { "id":1382219511, "time_played":"2013-10-19 23:51:51", "user_id":null, "track_id":73 }, { "id":1382219351, "time_played":"2013-10-19 23:49:11", "user_id":null, "track_id":77 }, { "id":1382218968, "time_played":"2013-10-19 23:42:48", "user_id":null, "track_id":82 }, { "id":1382218723, "time_played":"2013-10-19 23:38:43", "user_id":null, "track_id":76 }, { "id":1382218243, "time_played":"2013-10-19 23:30:43", "user_id":null, "track_id":79 }, { "id":1382217915, "time_played":"2013-10-19 23:25:15", "user_id":null, "track_id":75 }, { "id":1382217657, "time_played":"2013-10-19 23:20:57", "user_id":null, "track_id":84 }, { "id":1382217327, "time_played":"2013-10-19 23:15:27", "user_id":null, "track_id":74 }, { "id":1382217195, "time_played":"2013-10-19 23:13:15", "user_id":null, "track_id":81 }, { "id":1382216849, "time_played":"2013-10-19 23:07:29", "user_id":null, "track_id":78 } ] } </code></pre> <p>Now Both the Store.History and Store.Track records are gettings stored (see screenshot below)</p> <p><img src="https://i.stack.imgur.com/ng5qs.png" alt="enter image description here"></p> <p>However, When I check a record from the Store.History the "track" attribute returns null</p> <p><img src="https://i.stack.imgur.com/CEb6Z.png" alt="enter image description here"></p> <p>I have checked the Store.Track records and these contain the same ID's as presented in the JSON result</p> <p>EDIT: per request, these are my models:</p> <pre><code>var attr = DS.attr, belongsTo = DS.belongsTo, hasMany = DS.hasMany; Shoutzor.Album = DS.Model.extend({ artist: belongsTo('artist'), title: attr('string'), cover: attr('string') }); Shoutzor.Artist = DS.Model.extend({ name: attr('string'), profileimage: attr('string') }); Shoutzor.User = DS.Model.extend({ name: attr('string'), firstname: attr('string'), email: attr('string'), joined: attr('date'), last_active: attr('date') }); Shoutzor.Track = DS.Model.extend({ title: attr('string'), length: attr('number'), artist: hasMany('artist'), album: hasMany('album'), /* Convert the length in seconds to a string like '01:55' */ convertedLength: function() { var sec_num = parseInt(this.get('length'), 10); // don't forget the second parm var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var seconds = sec_num - (hours * 3600) - (minutes * 60); if (hours &lt; 10 &amp;&amp; hours &gt; 0) {hours = "0"+hours;} if (minutes &lt; 10 &amp;&amp; minutes &gt; 0) {minutes = "0"+minutes;} if (seconds &lt; 10) {seconds = "0"+seconds;} var time = ((hours != 0) ? hours + ':' : '') + ((minutes != 0) ? minutes +':' : '') + seconds; return time; }.property('length') }); Shoutzor.History = DS.Model.extend({ track: belongsTo('track'), user: belongsTo('user'), time_played: attr('date'), print_time: function() { var d = new Date(this.get('time_played')); var hours = (d.getHours() &lt; 10 ? "0" : '') + d.getHours(), minutes = (d.getMinutes() &lt; 10 ? "0" : '') + d.getMinutes(), seconds = (d.getSeconds() &lt; 10 ? "0" : '') + d.getSeconds(); return hours + ":" + minutes + ":" + seconds; }.property('time_played') }); </code></pre> <p>Any help is greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload