title: Rails Active Model Serializer - has_many and accessing the parent record
body: <p>I'm trying to build a JSON representation of some Rails models using Active Model Serializer, where some models embed others. For example, I have Event and Attendees, Event has_and_belongs_to_many Attendees.</p>
  
  <pre><code>class EventSerializer &lt; ActiveModel::Serializer
    attributes :name
  
    has_many :attendees, serializer: AttendeeSerializer
  end
  
  class AttendeeSerializer &lt; ActiveModel::Serializer
    attributes :name
  end
  </code></pre>
  
  <p>This would result in JSON like <code>{ name: 'Event One', attendees: [{ name: 'Alice' }, { name: 'Bob' }] }</code>.</p>
  
  <p>Now, I'd like to add what the attendees have said about the event. Let's say, Comment belongs_to Event, belongs_to Attendee. I'd like to include said comments in the serialized output of event, so it would become <code>{ name: 'Event One', attendees: [{ name: 'Alice', comments: [{ text: 'Event One was great!'}] }, { name: 'Bob', comments: [] }] }</code>.</p>
  
  <p>I could have</p>
  
  <pre><code>class AttendeeSerializer &lt; ActiveModel::Serializer
    attributes :name
  
    has_many :comments
  end
  </code></pre>
  
  <p>but that would select all the comments by this attendee for all the events - not what I want. I'd like to write this, but how do I find the particular event for which I'm doing serialization? Can I somehow access the 'parent' object, or maybe pass options to a has_many serializer?</p>
  
  <pre><code>class AttendeeSerializer &lt; ActiveModel::Serializer
    attributes :name
  
    has_many :comments
  
    def comments
      object.comments.where(event_id: the_event_in_this_context.id)
    end
  end
  </code></pre>
  
  <p>Is this something that can be done, or should I just build the JSON in another way for this particular use case?</p>