{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950/events","html_url":"https://github.com/rails-api/active_model_serializers/pull/950","id":87425287,"number":950,"title":"WIP - Deserializer implementation","user":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/0.10.x","name":"0.10.x","color":"bfd4f2"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Feature","name":"Feature","color":"009800"}],"state":"open","locked":false,"assignee":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones/5","html_url":"https://github.com/rails-api/active_model_serializers/milestones/0.10","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones/5/labels","id":1011188,"number":5,"title":"0.10","description":"","creator":{"login":"kurko","id":70312,"avatar_url":"https://avatars.githubusercontent.com/u/70312?v=3","gravatar_id":"","url":"https://api.github.com/users/kurko","html_url":"https://github.com/kurko","followers_url":"https://api.github.com/users/kurko/followers","following_url":"https://api.github.com/users/kurko/following{/other_user}","gists_url":"https://api.github.com/users/kurko/gists{/gist_id}","starred_url":"https://api.github.com/users/kurko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kurko/subscriptions","organizations_url":"https://api.github.com/users/kurko/orgs","repos_url":"https://api.github.com/users/kurko/repos","events_url":"https://api.github.com/users/kurko/events{/privacy}","received_events_url":"https://api.github.com/users/kurko/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":34,"state":"open","created_at":"2015-03-08T13:22:14Z","updated_at":"2015-08-18T17:36:35Z","due_on":null,"closed_at":null},"comments":20,"created_at":"2015-06-11T17:37:47Z","updated_at":"2015-08-19T23:49:22Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950","html_url":"https://github.com/rails-api/active_model_serializers/pull/950","diff_url":"https://github.com/rails-api/active_model_serializers/pull/950.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/950.patch"},"body":"We decided that it's time to work with deserialization. :tada::tada: :tada:  \r\n\r\nAMS aim to bring convention over configuration to JSON generation, but as it evolves into a more complete solution to build great APIs it makes sense to also handle deserialization.\r\n\r\nThe initial proposal of Deserialization is to convert your json input into something the ```ActiveRecord```  understands.\r\n\r\nIt will play really nice with json-api, enabling us to follow it conventions when creating and updating objects using AMS.\r\n\r\nHow it works:\r\n- [x] Automatically handle strong parameters, permitting the parameters that should be deserialized\r\n- [x] It identify the attributes that should be deserialized by your ```attributes``` method inside an existing serializer\r\n- [x] Allows you to override the parameters that should be serialized (ex. ```deserialize :title, :body```)\r\n- [ ] Work with the existing adapters conventions\r\n\r\n- JSON\r\n - [ ] Working CRUD\r\n - [ ] Parsing attributes\r\n - [ ] Parsing associations automatically\r\n\r\n- JSON API\r\n - [x] Working CRUD\r\n - [x] Parsing attributes\r\n - [x] Parsing associations automatically\r\n\r\nUsage:\r\n\r\n**post_serialization.rb**\r\n```ruby\r\nclass PostSerialization < ActiveModel::Serializer\r\n  attributes :id, :title, :body\r\n  belongs_to :user, root: :rows\r\n  params :title, :body\r\nend\r\n```\r\n\r\n**post_controller.rb**\r\n```ruby\r\nclass PostsController < ApplicationController\r\n\r\n  def create\r\n    @post = Post.new(post_params)\r\n\r\n    if @post.save\r\n      render json: @post, status: :created, location: @post\r\n    else\r\n      render json: @post.errors, status: :unprocessable_entity\r\n    end\r\n  end\r\n\r\n  private\r\n\r\n  def post_params\r\n    PostSerialization.deserialize(params)\r\n  end\r\nend\r\n```\r\n\r\n*This is a Working In Progress yet* :stuck_out_tongue_closed_eyes: "},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/132828293","html_url":"https://github.com/rails-api/active_model_serializers/pull/950#issuecomment-132828293","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950","id":132828293,"user":{"login":"GCorbel","id":1296201,"avatar_url":"https://avatars.githubusercontent.com/u/1296201?v=3","gravatar_id":"","url":"https://api.github.com/users/GCorbel","html_url":"https://github.com/GCorbel","followers_url":"https://api.github.com/users/GCorbel/followers","following_url":"https://api.github.com/users/GCorbel/following{/other_user}","gists_url":"https://api.github.com/users/GCorbel/gists{/gist_id}","starred_url":"https://api.github.com/users/GCorbel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GCorbel/subscriptions","organizations_url":"https://api.github.com/users/GCorbel/orgs","repos_url":"https://api.github.com/users/GCorbel/repos","events_url":"https://api.github.com/users/GCorbel/events{/privacy}","received_events_url":"https://api.github.com/users/GCorbel/received_events","type":"User","site_admin":false},"created_at":"2015-08-19T23:49:22Z","updated_at":"2015-08-19T23:49:22Z","body":"@joaomdmoura, you're right. I checked the code and decomposed it to find the bug and I introduced a new one.\r\n\r\nThis is what I do : \r\n\r\n    curl 'http://localhost:3000/api/v1/trails' -H 'Origin: http://localhost:4200' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: fr,en-US;q=0.8,en;q=0.6' -H 'CSP: active' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.130 Chrome/43.0.2357.130 Safari/537.36' -H 'Content-Type: application/vnd.api+json' -H 'Accept: application/vnd.api+json' -H 'Referer: http://localhost:4200/users' -H 'Connection: keep-alive' --data-binary '{\"data\":{\"relationships\":{\"users\":{\"data\":[{\"type\":\"users\",\"id\":\"2\"}]}},\"type\":\"trails\"}}' --compressed\r\n\r\nThis is a copy/paste from Chrome.\r\n\r\nThis is my code :  \r\n\r\n    module Api\r\n      module V1\r\n        class TrailsController < ApplicationController\r\n          def show\r\n            render json: Trail.find(params[:id])\r\n          end\r\n\r\n          def create\r\n            render json: { trail: Trail.create(trail_params) }\r\n          end\r\n\r\n          def update\r\n            trail = Trail.find(params[:id])\r\n            render json: trail.update_attributes(trail_params)\r\n          end\r\n\r\n          private\r\n\r\n          def trail_params\r\n            TrailSerialization.deserialize(params)\r\n          end\r\n        end\r\n      end\r\n    end\r\n\r\nAnd this is my error : \r\n\r\n    TypeError (no implicit conversion of String into Integer):\r\n      app/controllers/api/v1/trails_controller.rb:21:in `trail_params'\r\n      app/controllers/api/v1/trails_controller.rb:9:in `create'\r\n\r\nNot very useful so I decomposed your code to this : \r\n\r\n        def self.parse(params)\r\n          attrs, assoc = {}, {}\r\n          require 'pry'; binding.pry\r\n          attrs = params['attributes'] if params['attributes']\r\n          assoc = params['relationships'].map do |a|\r\n            key = a.shift\r\n            value = a.first['data']['type'].camelize.safe_constantize.find(a.shift['data']['id'])\r\n            { key => value }\r\n          end if params['relationships']\r\n          assoc.reduce attrs, :merge\r\n        end\r\n\r\nI fails at this line `value = a.first['data']['type'].camelize.safe_constantize.find(a.shift['data']['id'])`.\r\n\r\n`a.first['data']` gives `[{\"type\"=>\"users\", \"id\"=>\"2\"}]` so with `a.first['data']['type']` I have `TypeError: no implicit conversion of String into Integer`.\r\n\r\nI think it's a more useful description."}}