Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Polymorphic relationships #499

drogus opened this Issue · 7 comments

6 participants


It's quite common to use polymorphic associations for things like for example comments that can be added to multiple models.

The problem with implementing such relationship is that in the latest revisions it is required to have inverse association for every association. Additionally, the method which finds inverse relationship name relies only on classes.

For example, let's say we have an App.Comment model that can be associated with App.Post and App.User:

App.Post = DS.Model.extend
  comments: DS.hasMany('App.Comment')

App.User = DS.Model.extend
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  commentable: DS.belongsTo(polymorphic: true)

Of course commentable association will not work at the moment and there is no easy way to implement it on top of ember data, because there is no way to tell what is the inverse relationship. In order to do that, one of the two things needs to happen:

1) We allow to override getBelongsToName and getHasManyName easily - in such situation users could implement their own methods to get inverse association names.
2) Implement proper polymorphic associations - this would be the best option, but if it's not going to make it into ember-data soon, 1) is ok, too.

I'm happy to help with the implementation, but first I would like to get feedback.


Currently ActiveModel::Serializers will send down association: { type: 'ClassName', id: id } instead of just association: id for polymorphics - given the relationship between the two, would supporting something like that make sense? I assume this would be at the adapter level.


@nragaz I don't understand why wouldn't it make sense, could you elaborate on that? Adapter level is not enough at the moment, because you can't override a method of getting inverse relationship for given record.


@drogus I agree, it would make sense - I just wanted to bring those semantics into the conversation about an eventual implementation. With regard to the adapter, it just occurred to me that different adapters might want to use different representations for polymorphic associations, so assumptions about the data format shouldn't be hardcoded throughout the implementation. Since the REST adapter is supposed to be used with ActiveModel::Serializers, those assumptions should only affect that adapter.


@nragaz I agree that the data format for polymorphic associations should be configurable, as any other thing in the adapter, but for me this is just implementation detail - the important thing here is the API and behavior that will be used for polymorphic associations, data format would be probably flexible by default given how serializers and adapters work.


I for one wouldn't mind seeing this feature, @twinturbo and I added the polymorphic support to ActiveModel::Serializers with ember-data in mind. At that point in time, ember-data's relationship refactor was still in flux, so we decided wait until it landed and solidified.

@twinturbo thoughts on part 2 of our polymorphic trans-atlantic pairing session?


Polymorphic associations have been implemented in ember data ( see e4f7c37 )

Let me know if it works for you.

@ebryn ebryn closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.