{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/599","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/599/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/599/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/599/events","html_url":"https://github.com/rails-api/active_model_serializers/issues/599","id":40963744,"number":599,"title":"Optional attributes / Passing arbitrary params to serializer","user":{"login":"kenn","id":10350,"avatar_url":"https://avatars.githubusercontent.com/u/10350?v=3","gravatar_id":"","url":"https://api.github.com/users/kenn","html_url":"https://github.com/kenn","followers_url":"https://api.github.com/users/kenn/followers","following_url":"https://api.github.com/users/kenn/following{/other_user}","gists_url":"https://api.github.com/users/kenn/gists{/gist_id}","starred_url":"https://api.github.com/users/kenn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kenn/subscriptions","organizations_url":"https://api.github.com/users/kenn/orgs","repos_url":"https://api.github.com/users/kenn/repos","events_url":"https://api.github.com/users/kenn/events{/privacy}","received_events_url":"https://api.github.com/users/kenn/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":33,"created_at":"2014-08-23T01:17:25Z","updated_at":"2015-11-24T21:29:19Z","closed_at":null,"body":"(from #552) I just realized that AMS is in flux. But I'd throw in my issue (based on 0.9), anyway.\r\n\r\nWe have a master-detail view where the master shows a list of conversations with the last message included, and the detail displays one conversation with all messages.\r\n\r\nOne way to do it is:\r\n\r\n```ruby\r\nclass Conversation\r\n  has_many :messages\r\n  has_one :last_message\r\n  attr_accessor :show_details\r\nend\r\n\r\nclass ConversationSerializer < ActiveModel::Serializer\r\n  attributes :last_message, :messages\r\n\r\n  def attributes\r\n    if object.show_details\r\n      super.except(:last_message)\r\n    else\r\n      super.except(:messages)\r\n    end\r\n  end\r\nend\r\n\r\n# controller\r\n@conversation.show_details = true\r\nrender json: @conversation\r\n```\r\n\r\nBut this feels wrong on two fronts:\r\n\r\n* The model shouldn't contain view (or serializer, in this case) concerns like `show_details`. There should be a way to directly pass the hint from controller to serializer, not through model.\r\n* It doesn't work when it's called from an array context - you need to do `conversations.each{|i| i.show_details = true }`. The hint should be stored at the array level, not at the item level.\r\n\r\nFor those reason, I would suggest that we enable to pass arbitrary parameters to the serializer, from controller.\r\n\r\n```ruby\r\nclass ConversationSerializer < ActiveModel::Serializer\r\n  params :show_details\r\n\r\n  def attributes\r\n    if @show_details\r\n      super.except(:last_message)\r\n    else\r\n      super.except(:messages)\r\n    end\r\n  end\r\nend\r\n\r\n# controller\r\nrender json: @conversation, serializer_params: { show_details: true }\r\n```\r\n\r\nand pass down the params to each item, when the target was an array.\r\n\r\nThat way, we don't have to alter the model and separation can be done nicely. What do you think?"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/159410303","html_url":"https://github.com/rails-api/active_model_serializers/issues/599#issuecomment-159410303","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/599","id":159410303,"user":{"login":"beauby","id":2215778,"avatar_url":"https://avatars.githubusercontent.com/u/2215778?v=3","gravatar_id":"","url":"https://api.github.com/users/beauby","html_url":"https://github.com/beauby","followers_url":"https://api.github.com/users/beauby/followers","following_url":"https://api.github.com/users/beauby/following{/other_user}","gists_url":"https://api.github.com/users/beauby/gists{/gist_id}","starred_url":"https://api.github.com/users/beauby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beauby/subscriptions","organizations_url":"https://api.github.com/users/beauby/orgs","repos_url":"https://api.github.com/users/beauby/repos","events_url":"https://api.github.com/users/beauby/events{/privacy}","received_events_url":"https://api.github.com/users/beauby/received_events","type":"User","site_admin":false},"created_at":"2015-11-24T21:29:19Z","updated_at":"2015-11-24T21:29:19Z","body":"@cickes `instance_options` is only available in the master branch, not in the current RC. In the current RC, you have to use `options` instead."}}