Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Last active December 16, 2015 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philsturgeon/60aafc8787fe008fddf1 to your computer and use it in GitHub Desktop.
Save philsturgeon/60aafc8787fe008fddf1 to your computer and use it in GitHub Desktop.
Using AMS? Whitelist includes to avoid overuse and n+1 slowness
# Somewhere, maybe in ApplicationController
def whitelist_relationships(params)
whitelist = %w(driver seats driver.avatar)
params[:include].split(/,/) & whitelist if params[:include].present?
end
# Then when rendering the resource
render json: Array(@resources), root: :trips, include: whitelist_relationships(params)
@filipebarcos
Copy link

params[:include] might have an array, instead of a String.

whitelist = %w(...)
params[:include] = params[:include].split(/,/) if params[:include].is_a?(String)
params[:include] & whitelist

@filipebarcos
Copy link

That doesn't work either, because of nil

@nhocki
Copy link

nhocki commented Dec 16, 2015

whitelist = %w(...)
params[:include] = params[:include].split(/,/) if params[:include].is_a?(String)
Array(params[:include]) & whitelist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment