{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/77549376","id":77549376,"diff_hunk":"@@ -21,12 +24,56 @@ class MyModel\n end\n ```\n \n-Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner.  The above code now becomes:\n+### Inheriting ActiveModelSerializers::Model\n+Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner.\n+The above code now becomes:\n ```ruby\n # my_model.rb\n class MyModel < ActiveModelSerializers::Model\n   attr_accessor :id, :name, :level\n end\n ```\n \n-The default serializer would be `MyModelSerializer`.\n\\ No newline at end of file\n+The default serializer would be `MyModelSerializer`.\n+\n+### Serializing an instance\n+Sometimes you don't want to create yet another class just to serialize an object.\n+Well, athough it is recommended to have defined classes for what you serialize,\n+this is possible as well:\n+\n+Given the following serializer:\n+\n+```ruby\n+class Api::V1::UserSerializer\n+  type :user #this is necessery, otherwise AMS will show your instance class name\n+\n+  attributes :id, :name\n+```\n+\n+```ruby\n+class Api::V1::UsersController\n+  def show\n+    #this is just an example\n+    an_instance = OpenStruct.new(id: 1, name: 'Just an example')\n+\n+    an_instance.singleton_class.send(:alias_method, :read_attribute_for_serialization, :send)\n+\n+    render jsonapi: an_instance, serializer: Api::V1::UserSerializer\n+  end\n+end\n+```\n+\n+If your object is just a hash you can do the following trick:","path":"docs/howto/serialize_poro.md","position":65,"original_position":65,"commit_id":"fdd9970df44e21d5fe513ece580d34f60b86e906","original_commit_id":"fdd9970df44e21d5fe513ece580d34f60b86e906","user":{"login":"bf4","id":142914,"avatar_url":"https://avatars.githubusercontent.com/u/142914?v=3","gravatar_id":"","url":"https://api.github.com/users/bf4","html_url":"https://github.com/bf4","followers_url":"https://api.github.com/users/bf4/followers","following_url":"https://api.github.com/users/bf4/following{/other_user}","gists_url":"https://api.github.com/users/bf4/gists{/gist_id}","starred_url":"https://api.github.com/users/bf4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bf4/subscriptions","organizations_url":"https://api.github.com/users/bf4/orgs","repos_url":"https://api.github.com/users/bf4/repos","events_url":"https://api.github.com/users/bf4/events{/privacy}","received_events_url":"https://api.github.com/users/bf4/received_events","type":"User","site_admin":false},"body":"I would like to strongly recommend not trying to serialize primitives like Hashes.  AMS is intended to models.  There's a lot of parts of the AMS contract that would be weird to have on a hash.  https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb","created_at":"2016-09-05T18:23:53Z","updated_at":"2016-09-05T18:23:53Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/1910#discussion_r77549376","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/77549376"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1910#discussion_r77549376"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910","id":83902736,"html_url":"https://github.com/rails-api/active_model_serializers/pull/1910","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1910.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1910.patch","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1910","number":1910,"state":"open","locked":false,"title":"Enhance PORO documentation","user":{"login":"vasilakisfil","id":590575,"avatar_url":"https://avatars.githubusercontent.com/u/590575?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilakisfil","html_url":"https://github.com/vasilakisfil","followers_url":"https://api.github.com/users/vasilakisfil/followers","following_url":"https://api.github.com/users/vasilakisfil/following{/other_user}","gists_url":"https://api.github.com/users/vasilakisfil/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilakisfil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilakisfil/subscriptions","organizations_url":"https://api.github.com/users/vasilakisfil/orgs","repos_url":"https://api.github.com/users/vasilakisfil/repos","events_url":"https://api.github.com/users/vasilakisfil/events{/privacy}","received_events_url":"https://api.github.com/users/vasilakisfil/received_events","type":"User","site_admin":false},"body":"#### Purpose\r\nI found it a bit frustrating sometimes when I just wanted to serialize an object using an AMS serializer, but I first had to define a class for it and either inherit or duck type the needed AMS methods. I showcase how this can be done quickly and dirty but I explicitly say that it is not recommended.\r\n\r\n#### Changes\r\nEnhance PORO documentation\r\n\r\n#### Caveats\r\n\r\n\r\n#### Related GitHub issues\r\n\r\n\r\n#### Additional helpful information\r\nSometimes I felt that POROs (its methods actually that have the same name with the serializer defined attributes) and Hashes (the values which belong to keys which have the same name with the serializer defined attributes) should be serialized automatically without doing anything when specifying a serializer.\r\n\r\n\r\n","created_at":"2016-09-03T18:42:47Z","updated_at":"2016-09-05T18:23:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42c36e8f0be679aa55e2d266952e6a3e61218f40","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910/comments","review_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1910/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/fdd9970df44e21d5fe513ece580d34f60b86e906","head":{"label":"vasilakisfil:docs/serialize_poro","ref":"docs/serialize_poro","sha":"fdd9970df44e21d5fe513ece580d34f60b86e906","user":{"login":"vasilakisfil","id":590575,"avatar_url":"https://avatars.githubusercontent.com/u/590575?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilakisfil","html_url":"https://github.com/vasilakisfil","followers_url":"https://api.github.com/users/vasilakisfil/followers","following_url":"https://api.github.com/users/vasilakisfil/following{/other_user}","gists_url":"https://api.github.com/users/vasilakisfil/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilakisfil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilakisfil/subscriptions","organizations_url":"https://api.github.com/users/vasilakisfil/orgs","repos_url":"https://api.github.com/users/vasilakisfil/repos","events_url":"https://api.github.com/users/vasilakisfil/events{/privacy}","received_events_url":"https://api.github.com/users/vasilakisfil/received_events","type":"User","site_admin":false},"repo":{"id":67296605,"name":"active_model_serializers","full_name":"vasilakisfil/active_model_serializers","owner":{"login":"vasilakisfil","id":590575,"avatar_url":"https://avatars.githubusercontent.com/u/590575?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilakisfil","html_url":"https://github.com/vasilakisfil","followers_url":"https://api.github.com/users/vasilakisfil/followers","following_url":"https://api.github.com/users/vasilakisfil/following{/other_user}","gists_url":"https://api.github.com/users/vasilakisfil/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilakisfil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilakisfil/subscriptions","organizations_url":"https://api.github.com/users/vasilakisfil/orgs","repos_url":"https://api.github.com/users/vasilakisfil/repos","events_url":"https://api.github.com/users/vasilakisfil/events{/privacy}","received_events_url":"https://api.github.com/users/vasilakisfil/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vasilakisfil/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/vasilakisfil/active_model_serializers","forks_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/forks","keys_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/events","assignees_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/merges","archive_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/releases{/id}","deployments_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/deployments","created_at":"2016-09-03T15:15:09Z","updated_at":"2016-09-03T15:15:10Z","pushed_at":"2016-09-05T15:11:02Z","git_url":"git://github.com/vasilakisfil/active_model_serializers.git","ssh_url":"git@github.com:vasilakisfil/active_model_serializers.git","clone_url":"https://github.com/vasilakisfil/active_model_serializers.git","svn_url":"https://github.com/vasilakisfil/active_model_serializers","homepage":"","size":3687,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"1dc2b74059731339f4df3e7882fd949e20433013","user":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"repo":{"id":2889230,"name":"active_model_serializers","full_name":"rails-api/active_model_serializers","owner":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails-api/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":false,"url":"https://api.github.com/repos/rails-api/active_model_serializers","forks_url":"https://api.github.com/repos/rails-api/active_model_serializers/forks","keys_url":"https://api.github.com/repos/rails-api/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails-api/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails-api/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/rails-api/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/events","assignees_url":"https://api.github.com/repos/rails-api/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/rails-api/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/rails-api/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/rails-api/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/rails-api/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails-api/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/rails-api/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails-api/active_model_serializers/merges","archive_url":"https://api.github.com/repos/rails-api/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails-api/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/rails-api/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/rails-api/active_model_serializers/releases{/id}","deployments_url":"https://api.github.com/repos/rails-api/active_model_serializers/deployments","created_at":"2011-12-01T06:39:26Z","updated_at":"2016-09-05T13:08:19Z","pushed_at":"2016-09-05T13:51:49Z","git_url":"git://github.com/rails-api/active_model_serializers.git","ssh_url":"git@github.com:rails-api/active_model_serializers.git","clone_url":"https://github.com/rails-api/active_model_serializers.git","svn_url":"https://github.com/rails-api/active_model_serializers","homepage":"","size":5354,"stargazers_count":3738,"watchers_count":3738,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1004,"mirror_url":null,"open_issues_count":129,"forks":1004,"open_issues":129,"watchers":3738,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1910"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1910"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1910/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910/comments"},"review_comment":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1910/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/fdd9970df44e21d5fe513ece580d34f60b86e906"}}}}
{"actor":{"display_login":"bf4"}}