Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks polymorphic relationships in Ember Data #25

Open
mcm-ham opened this issue Jan 17, 2015 · 4 comments
Open

Breaks polymorphic relationships in Ember Data #25

mcm-ham opened this issue Jan 17, 2015 · 4 comments
Labels

Comments

@mcm-ham
Copy link
Contributor

mcm-ham commented Jan 17, 2015

With postgres_ext-serializers installed polymorphic relationships are serialized as:

images: [{
  id: 1,
  imageable_id: 1,
  imageable_type: 'User'
}]

Which ember data does not support: emberjs/data#1574
Without postgres_ext-serializers installed polymorphic relationships are serialized as:

images: [{
  id: 1,
  imageable: {
    id: 1,
    type: 'User'
  }
}]

Which ember data does support.

@BenMorganIO
Copy link

I feel like this could be a simple fix. Is there a way to detect if the object is polymorphic?

@felixbuenemann
Copy link
Collaborator

This could probably be solved by reflecting on the association.

@felixbuenemann
Copy link
Collaborator

It seems this was fixed in emberjs/data#1662 so imageable_id and imageable_type should just work.

@felixbuenemann
Copy link
Collaborator

Adding proper support for polymorphic associations also requires mangling the content of the type column.

Example:

module Foo
  class Asset
    belongs_to :viewable
  end

  class User
  end

  class AssetSerializer
    embed :ids, include: true
    attributes :id
    has_one :viewable, polymorphic: true
  end

  class UserSerializer
    attributes :id
  end
end

user = Foo::User.create
=> #<Foo::User id:1>
asset = Foo::Asset.create(viewable: user)
=> #<Foo::Asset id:1, viewable_id: 1, viewable_type: "Foo::User">
Foo::AssetSerializer.new(asset).as_json
=> {:users=> [{:id=>1}], :asset=>{:id=>1, :viewable=>{:type=>:user, :id=>1}}}

Resulting JSON:

{
   "users" : [
      {
         "id" : 1
      }
   ],
   "asset" : {
      "id" : 1,
      "viewable" : {
         "type" : "user",
         "id" : 1
      }
   }
}

As can be seen, AMS removes the module names from the types and changes them to underscored notation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants