Skip to content

Instantly share code, notes, and snippets.

@jnx
Created August 24, 2012 06:06
Show Gist options
  • Save jnx/3446339 to your computer and use it in GitHub Desktop.
Save jnx/3446339 to your computer and use it in GitHub Desktop.
Ember Associations
App.Comment = DS.Model.extend(
body: DS.attr("string")
post: DS.belongsTo('App.Post')
validate: ->
if @get("body") is `undefined` or @get("body") is ""
"Comments require a body."
)
class CommentSerializer < ActiveModel::Serializer
attributes :body, :id, :post_id
end
App.Post = DS.Model.extend(
title: DS.attr("string")
body: DS.attr("string")
comments: DS.hasMany('App.Comment')
validate: ->
if @get("title") is `undefined` or @get("title") is "" or @get("body") is `undefined` or @get("body") is ""
"Posts require a title and body."
)
class PostSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :title, :body, :id
has_many :comments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment