Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I now use two different ways of working with rails-style "polymorphic" models. I have updated the code below to show both uses.</p> <p><strong><code>Attachment</code> model</strong>: This is "polymorphic" on the Rails side but is always "embedded" on the Ember side. The reason for this is that I currently only need to save/update attachments along with their associated model.</p> <p><strong><code>Comment</code> model</strong>: This is polymorphic on both the Rails side and the Ember side.</p> <p>I have also included code for the <code>Post</code> model as it can have multiple attachments and multiple comments.</p> <hr> <p><strong>Rails code:</strong></p> <pre><code>class Attachment &lt; ActiveRecord::Base belongs_to :user belongs_to :attachable, polymorphic: true end class Comment &lt; ActiveRecord::Base belongs_to :user belongs_to :commentable, polymorphic: true end class Post &lt; ActiveRecord::Base belongs_to :user has_many :attachments, as: :attachable has_many :comments, as: :commentable end class ApplicationSerializer &lt; ActiveModel::Serializer embed :ids, include: true end class AttachmentSerializer &lt; ApplicationSerializer attributes :id, :url, :errors has_many :comments end class CommentSerializer &lt; ApplicationSerializer attributes :id, :body, :created_at, :commentable_id, :commentable_type has_one :user end class PostSerializer &lt; ApplicationSerializer attributes :id, :title, :body, :posted_at, :errors has_one :user has_many :attachments, embed: :objects, include: true has_many :comments end class Api::V1::PostsController &lt; Api::V1::BaseController before_filter :auth_only!, only: :create def create # clean / capture ember-data supplied arguments params[:post].delete(:user_id) attachments_params = params[:post].delete(:attachments) @post = current_user.posts.new(params[:post]) process_attachments(attachments_params) if @post.save render json: @post, status: 201 else warden.custom_failure! render json: @post, status: 422 end end protected def process_attachments(attachments_params) return unless attachments_params.present? attachments_params.each do |attachment_params| # ignore ember-data's additional keys attachment_params.delete(:created_at) attachment_params.delete(:user_id) attachment = @post.attachments.new(attachment_params) attachment.user = current_user end end end </code></pre> <p><strong>Ember code:</strong></p> <pre><code>DS.RESTAdapter.configure 'App.Post', alias: 'Post' DS.RESTAdapter.map 'App.Post', attachments: { embedded: 'always' } App.Store = DS.Store.extend adapter: DS.RESTAdapter.create namespace: 'api/v1' App.Comment = App.Model.extend user: DS.belongsTo('App.User') commentable: DS.belongsTo('App.Commentable', { polymorphic: true }) body: DS.attr('string') createdAt: DS.attr('date') App.Commentable = App.Model.extend comments: DS.hasMany('App.Comment') App.Attachment = App.Commentable.extend user: DS.belongsTo('App.User') url: DS.attr('string') App.Post = App.Commentable.extend user: DS.belongsTo('App.User') attachments: DS.hasMany('App.Attachment') title: DS.attr('string') body: DS.attr('string') postedAt: DS.attr('date') App.PostFormOverlayController = App.OverlayController.extend # 'files' attribute is set by a widget that wraps the filepicker.io JS updateAttachments: (-&gt; attachments = @get('attachments') attachments.clear() @get('files').forEach (file) =&gt; attachment = App.Attachment.createRecord({fpFile: file}) attachments.addObject(attachment) ).observes('files') App.CommentNewController = App.ObjectController.extend # this should be instantiated with it's model set to the commentable # item. eg. `{{render 'comment/new' content}}` save: -&gt; transaction = @get('store').transaction() comment = transaction.createRecord App.Comment, body: @get('body') commentable: @get('model') comment.one 'didCreate', @, -&gt; @set('body', null) transaction.commit() </code></pre> <p>Unfortunately my Rails code has become a little polluted with ember-data specific oddities as it tries to send back all the attributes that are defined on the models. (<strong>Note</strong>: There is an <a href="https://gist.github.com/tomdale/4263171" rel="noreferrer">open proposal</a> for read-only attributes that would solve the params pollution issue)</p> <p>If anyone knows a better way to approach any of the above please let me know!</p> <p><strong>NB:</strong> I'm a little concerned that having models extend from <code>App.Commentable</code> will prevent me from having multiple polymorphic attachments on a model, I may need to look for a different way of handling that.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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