Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to get hasMany association
    text
    copied!<p>I used commit <a href="https://github.com/emberjs/ember.js/commit/eaa11231062e93567c9a1406415c8f21c4792e6b" rel="noreferrer">eaa1123</a> (ember) and <a href="https://github.com/emberjs/data/commit/508479dee764c2e9926487131a5a4814cfda1bf6" rel="noreferrer">508479d</a> (ember-data) to build the JS files.</p> <p>I have the following JSON returned from my Rails backend, which is generated with <a href="https://github.com/rails-api/active_model_serializers" rel="noreferrer">active_model_serializers</a> (0.6.0):</p> <pre><code>{ "posts": [ { "id": 408, "title": "Lorem Ipsum", "body": "In at quo tempora provident nemo.", "comments": [ { "id": 956, "body": "Quo incidunt eum dolorem." }, ... ] } ] } </code></pre> <p>and the following Ember models:</p> <pre><code>App.Post = DS.Model.extend({ title: DS.attr('string'), body: DS.attr('string'), comments: DS.hasMany('App.Comment', { embedded: true }) }); App.Comment = DS.Model.extend({ body: DS.attr('string'), post: DS.belongsTo('App.Post') }); </code></pre> <p>All look perfectly normal:</p> <pre><code>post = App.Post.find(408); post.get('title') // =&gt; "Lorem Ipsum" </code></pre> <p>However, I can't seem to get to the comments:</p> <pre><code>comments = post.get('comments') comments.get('firstObject') instanceof App.Comment // =&gt; true comments.forEach(function(comment) { console.log(comment.get('body')) }) //=&gt; undefined </code></pre> <p>When I use:</p> <pre><code>comments.content </code></pre> <p>I get an Array that contain objects, so:</p> <pre><code>comments.content[0] //=&gt; { body: "Quo incidunt eum dolorem.", id: 956 } </code></pre> <p>but this is not what I expected.</p> <p>It seems so obvious, so I must be doing something wrong. As a side-effect: currently I'm not able to render my comments in a template in a easy way, so I hope someone can help me on this one.</p> <p>Thanks in advance.</p>
 

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