{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/927","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/927/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/927/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/927/events","html_url":"https://github.com/rails-api/active_model_serializers/issues/927","id":79775399,"number":927,"title":"Load associations to one level while conditionally sideloading associations in Active model serializers","user":{"login":"ovamsikrishna","id":1748896,"avatar_url":"https://avatars.githubusercontent.com/u/1748896?v=3","gravatar_id":"","url":"https://api.github.com/users/ovamsikrishna","html_url":"https://github.com/ovamsikrishna","followers_url":"https://api.github.com/users/ovamsikrishna/followers","following_url":"https://api.github.com/users/ovamsikrishna/following{/other_user}","gists_url":"https://api.github.com/users/ovamsikrishna/gists{/gist_id}","starred_url":"https://api.github.com/users/ovamsikrishna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ovamsikrishna/subscriptions","organizations_url":"https://api.github.com/users/ovamsikrishna/orgs","repos_url":"https://api.github.com/users/ovamsikrishna/repos","events_url":"https://api.github.com/users/ovamsikrishna/events{/privacy}","received_events_url":"https://api.github.com/users/ovamsikrishna/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-05-23T08:13:46Z","updated_at":"2015-05-29T06:43:51Z","closed_at":null,"body":"This ain't an issue frankly speaking. May be a tailored feature request. Tried my best to get this working but in vain.\r\nUsing AMS version 0.8.3.\r\nI created a base_serializer.rb like this and extended the same.\r\n\r\n    class BaseSerializer < ActiveModel::Serializer\r\n      def include_associations!\r\n        if @options[:embed]\r\n          embed = @options[:embed].split(',').map{|item| item.strip.to_sym}\r\n          embed.each do |assoc|\r\n            include! assoc if _associations.keys.include?(assoc)\r\n          end\r\n        end\r\n      end\r\n    end\r\n\r\n    class EventSerializer < BaseSerializer\r\n      attributes :id, :name\r\n      has_many :organizers, serializer: OrganizerSerializer\r\n      has_many :participants, serializer: ParticipantSerializer\r\n    end\r\n\r\n    class OrganizerSerializer < BaseSerializer\r\n      attributes :id, :name\r\n      has_many :related, serializer: RelatedSerializer\r\n    end\r\n\r\n    class ParticipantSerializer < BaseSerializer\r\n      attributes :id, :name\r\n      has_many :related, serializer: RelatedSerializer\r\n    end\r\n\r\n    class RelatedSerializer < BaseSerializer\r\n      attributes :id, :name\r\n      has_many :something, serializer: SomethingSerializer\r\n    end\r\n\r\nand the index method in EventsController is written as\r\n\r\n    # GET /events?embed=organizers,participants\r\n      def index\r\n          @events = Event.all\r\n          render json: @events, embed: params[:embed]\r\n      end\r\n\r\nWith this I can get the :id and :name of events, organizers and participants. But, I want the attributes of related association as well. I don't need details of something serializer. I want to go till this level for each association. How can I achieve that?"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/106711012","html_url":"https://github.com/rails-api/active_model_serializers/issues/927#issuecomment-106711012","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/927","id":106711012,"user":{"login":"ovamsikrishna","id":1748896,"avatar_url":"https://avatars.githubusercontent.com/u/1748896?v=3","gravatar_id":"","url":"https://api.github.com/users/ovamsikrishna","html_url":"https://github.com/ovamsikrishna","followers_url":"https://api.github.com/users/ovamsikrishna/followers","following_url":"https://api.github.com/users/ovamsikrishna/following{/other_user}","gists_url":"https://api.github.com/users/ovamsikrishna/gists{/gist_id}","starred_url":"https://api.github.com/users/ovamsikrishna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ovamsikrishna/subscriptions","organizations_url":"https://api.github.com/users/ovamsikrishna/orgs","repos_url":"https://api.github.com/users/ovamsikrishna/repos","events_url":"https://api.github.com/users/ovamsikrishna/events{/privacy}","received_events_url":"https://api.github.com/users/ovamsikrishna/received_events","type":"User","site_admin":false},"created_at":"2015-05-29T06:43:51Z","updated_at":"2015-05-29T06:43:51Z","body":"This is perfect @groyoh . Thanks a lot. May be I'm asking for too much now. It is a problem from my end actually. the assoc_serializer might be nil at times where we don't specify any serializer option to associations at times. As of now I added a present? check for assoc_serializer. What can I do in those cases? Do we have any option to fetch the serializer when no serializer option is provided?\r\n\r\nSay something like\r\n```\r\nhas_many :roles\r\n```\r\nThis association actually uses the RoleSerializer by default way. I don't specify any serializer option here.\r\n\r\n```\r\nclass BaseSerializer < ActiveModel::Serializer\r\n  def include_associations!\r\n    @options[:embed_level] ||= 1\r\n    return unless @options.key?(:embed) && @options[:embed_level] != 0\r\n    embed = @options[:embed].split(',').map{|item| item.strip.to_sym}\r\n    embed.each do |assoc|\r\n      next unless _associations.key?(assoc)\r\n      assoc_serializer = _associations[assoc].options[:serializer]\r\n      embed = @options[:embed]\r\n      embed_level = @options[:embed_level]\r\n      @options[:embed_level] -= 1\r\n      @options[:embed] = assoc_serializer._associations.keys.join(\",\") if assoc_serializer.present?\r\n      include! assoc\r\n      @options[:embed] = embed\r\n      @options[:embed_level] = embed_level\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nWell, with this solution you actually made my day..."}}