mardi 24 février 2015

How to define a polymorphic association?

I am trying to set a polymorphic association. Unfortunately, the Ember docs don't cover this too well.


My API (Rails) has the following definition:



class User < ActiveRecord::Base
belongs_to :profile, polymorphic: true#, dependent: :destroy
end

class MemberProfile < ActiveRecord::Base
has_one :user, as: :profile, dependent: :destroy
end

class GuestProfile < ActiveRecord::Base
has_one :user, as: :profile, dependent: :destroy
end


Which means, in Rails console, I can do User.last.profile, which will return either a MemberProfile or GuestProfile object.


How should I define this on the client side (Ember)?



// app/models/user.js
import DS from 'ember-data';
export default DS.Model.extend({
profile: DS.belongsTo('profile', { polymorphic: true, async: true })
});

// app/models/member-profile.js
import DS from 'ember-data';
export default DS.Model.extend({
profile: DS.belongsTo('profile', { polymorphic: true, async: true })
});

// app/models/guest-profile.js
import DS from 'ember-data';
export default DS.Model.extend({
profile: DS.belongsTo('profile', { polymorphic: true, async: true })
});




Aucun commentaire:

Enregistrer un commentaire