Skip to content

Instantly share code, notes, and snippets.

@benmoss
Last active January 3, 2016 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmoss/8460845 to your computer and use it in GitHub Desktop.
Save benmoss/8460845 to your computer and use it in GitHub Desktop.
class Post
belongs_to :poster, polymorphic: true
end
class User
has_many :posts, as: :poster
end
class Admin
has_many :posts, as: :poster
end
class PostSerializer
attributes :poster
has_one :poster, polymorphic: true, embed: :ids, include: true
end
PostSerializer.new(Post.last).as_json
# would be:
# {"post" => {poster: {id: 1, type: "admin"}},
# "admins" => [{id: 1]}}
# want to make it:
# {"post" => {poster: {id: 1, type: "superuser"}},
# "superusers" => [{id: 1]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment