title: Sideloading conditionally with ActiveModel::Serializers
body: <p>I'm building an API using <strong>ActiveModel::Serializers</strong>. What is the best way to sideload data conditionally using params?</p>
  
  <p>So I can make requests like <code>GET /api/customers</code>:</p>
  
  <pre><code>"customers": {
     "first_name": "Bill",
     "last_name": "Gates"
  }
  </code></pre>
  
  <p>And <code>GET /api/customers?embed=address,note</code></p>
  
  <pre><code>"customers": {
     "first_name": "Bill",
     "last_name": "Gates"
  },
  "address: {
     "street": "abc"
  },
  "note": {
     "body": "Banned"
  }
  </code></pre>
  
  <p>Something like that depending on the params. I know ActiveModel::Serializers has the <code>include_[ASSOCIATION]?</code> syntax but how can I use it efficiently from my controllers?</p>
  
  <hr>
  
  <p>This is my current solution, but it's not neat:</p>
  
  <p><strong>customer_serializer.rb:</strong></p>
  
  <pre><code>def include_address?
    !options[:embed].nil? &amp;&amp; options[:embed].include?(:address)
  end
  </code></pre>
  
  <p><strong>application_controller.rb:</strong></p>
  
  <pre><code>def embed_resources(resources = [])
    params[:embed].split(',').map { |x| resources &lt;&lt; x.to_sym } if params[:embed]
    resources
  end
  </code></pre>
  
  <p><strong>customers_controller.rb:</strong></p>
  
  <pre><code>def show
    respond_with @customer, embed: embed_resources
  end
  </code></pre>
  
  <p>Must be an easier way?</p>