Note that there are some explanatory texts on larger screens.

plurals
  1. POSide-loading objects with non-standard class names in EmberJS with Rails+active_model_serializers
    primarykey
    data
    text
    <p>I have a few models in Rails that look something like this:</p> <pre><code>class Issue &lt; ActiveRecord::Base belongs_to :reporter, class_name: 'User' belongs_to :assignee, class_name: 'User' has_many :comments end class User &lt; ActiveRecord::Base end class Comment &lt; ActiveRecord::Base end </code></pre> <p>with serializers like so:</p> <pre><code>class IssueSerializer &lt; ActiveModel::Serializer attributes :id embed :ids, include: true has_one :reporter, :embed =&gt; :ids has_one :assignee, :embed =&gt; :ids end class UserSerializer &lt; ActiveModel::Serializer attributes :id, :name end class CommentSerializer &lt; ActiveModel::Serializer attributes :id, :body end </code></pre> <p>This produces JSON that looks something like the following:</p> <pre><code>{ "issues": [ { "id": 6, "reporter_id": 1, "assignee_id": 2, "comment_ids": [ 3 ] }, ], "comments": [ { "id": 3 "body": "Great comment" } ], "reporters": [ { "id": 1 "name": "Ben Burton" } ], "assignees": [ { "id": 2 "name": "Jono Mallanyk" } ] } </code></pre> <p>The problem is that the side-loaded reporters and assignees JSON objects aren't recognized by Ember as User objects, and I see the following error:</p> <pre><code>Uncaught Error: assertion failed: Your server returned a hash with the key reporters but you have no mapping for it </code></pre> <p>I don't want to remove</p> <pre><code>embed :ids, include: true </code></pre> <p>from my IssueSerializer, because then the comments wouldn't be serialized.</p> <p>There are a few potential ways to solve this that I've considered:</p> <ul> <li>If the embed method for ActiveModel::Serializer accepted a list of models in its include option, this could filter the JSON response to only include side-loaded comments.</li> <li>Ember data's model could be configured to side load users from "users", "reporters" and "assignees"... but as far as I can tell from the source it only appears to support one key for sideloadAs.</li> <li>Somehow ignore/disable side-loading errors for unknown keys (probably the least sane approach).</li> </ul> <p>Is there another option I'm missing here? I think I may have to write a fix and submit a pull request to either rails-api/active_model_serializers, emberjs/data, or both.</p> <p><strong>EDIT</strong>: My temporary workaround for this is to change the IssueSerializer to only serialize the ids for reporter and assignee:</p> <pre><code>class IssueSerializer &lt; ActiveModel::Serializer attributes :id, :reporter_id, :assignee_id embed :ids, include: true has_many :comments, :embed =&gt; :ids end </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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