{"id":5766,"date":"2014-04-06T20:17:40","date_gmt":"2014-04-06T20:17:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/side-loading-objects-with-non-standard-class-names-in-emberjs-with-railsactive_model_serializers-collection-of-common-programming-errors\/"},"modified":"2014-04-06T20:17:40","modified_gmt":"2014-04-06T20:17:40","slug":"side-loading-objects-with-non-standard-class-names-in-emberjs-with-railsactive_model_serializers-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/side-loading-objects-with-non-standard-class-names-in-emberjs-with-railsactive_model_serializers-collection-of-common-programming-errors\/","title":{"rendered":"Side-loading objects with non-standard class names in EmberJS with Rails+active_model_serializers-Collection of common programming errors"},"content":{"rendered":"<p>I have a few models in Rails that look something like this:<\/p>\n<pre><code>class Issue &lt; ActiveRecord::Base\n  belongs_to :reporter, class_name: 'User'\n  belongs_to :assignee, class_name: 'User'\n  has_many :comments\nend\n\nclass User &lt; ActiveRecord::Base\nend\n\nclass Comment &lt; ActiveRecord::Base\nend\n<\/code><\/pre>\n<p>with serializers like so:<\/p>\n<pre><code>class IssueSerializer &lt; ActiveModel::Serializer\n  attributes :id\n  embed :ids, include: true\n\n  has_one :reporter, :embed =&gt; :ids\n  has_one :assignee, :embed =&gt; :ids\nend\n\nclass UserSerializer &lt; ActiveModel::Serializer\n  attributes :id, :name\nend\n\nclass CommentSerializer &lt; ActiveModel::Serializer\n  attributes :id, :body\nend\n<\/code><\/pre>\n<p>This produces JSON that looks something like the following:<\/p>\n<pre><code>{\n  \"issues\": [\n    {\n      \"id\": 6,\n      \"reporter_id\": 1,\n      \"assignee_id\": 2,\n      \"comment_ids\": [\n        3\n      ]\n    },\n  ],\n  \"comments\": [\n    {\n      \"id\": 3\n      \"body\": \"Great comment\"\n    }\n  ],\n  \"reporters\": [\n    {\n      \"id\": 1\n      \"name\": \"Ben Burton\"\n    }\n  ],\n  \"assignees\": [\n    {\n      \"id\": 2\n      \"name\": \"Jono Mallanyk\"\n    }\n  ]\n}\n<\/code><\/pre>\n<p>The problem is that the side-loaded reporters and assignees JSON objects aren&#8217;t recognized by Ember as User objects, and I see the following error:<\/p>\n<pre><code>Uncaught Error: assertion failed: Your server returned a hash with the key reporters but you have no mapping for it\n<\/code><\/pre>\n<p>I don&#8217;t want to remove<\/p>\n<pre><code>embed :ids, include: true\n<\/code><\/pre>\n<p>from my IssueSerializer, because then the comments wouldn&#8217;t be serialized.<\/p>\n<p>There are a few potential ways to solve this that I&#8217;ve considered:<\/p>\n<ul>\n<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>\n<li>Ember data&#8217;s model could be configured to side load users from &#8220;users&#8221;, &#8220;reporters&#8221; and &#8220;assignees&#8221;&#8230; but as far as I can tell from the source it only appears to support one key for sideloadAs.<\/li>\n<li>Somehow ignore\/disable side-loading errors for unknown keys (probably the least sane approach).<\/li>\n<\/ul>\n<p>Is there another option I&#8217;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>\n<p><strong>EDIT<\/strong>: My temporary workaround for this is to change the IssueSerializer to only serialize the ids for reporter and assignee:<\/p>\n<pre><code>class IssueSerializer &lt; ActiveModel::Serializer\n  attributes :id, :reporter_id, :assignee_id\n  embed :ids, include: true\n\n  has_many :comments, :embed =&gt; :ids\nend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a few models in Rails that look something like this: class Issue &lt; ActiveRecord::Base belongs_to :reporter, class_name: &#8216;User&#8217; belongs_to :assignee, class_name: &#8216;User&#8217; has_many :comments end class User &lt; ActiveRecord::Base end class Comment &lt; ActiveRecord::Base end with serializers like so: class IssueSerializer &lt; ActiveModel::Serializer attributes :id embed :ids, include: true has_one :reporter, :embed =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5766","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5766"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5766\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}