Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Last active December 23, 2015 13:59
Show Gist options
  • Select an option

  • Save westonplatter/6645878 to your computer and use it in GitHub Desktop.

Select an option

Save westonplatter/6645878 to your computer and use it in GitHub Desktop.
Preforms a Double Nested includes to save us from N+1 queries. Used the bullet gem (https://github.com/flyerhzm/bullet) to fine tune this => thanks @flyerhzm
# //////////////////////////////////////////////////////
#
# spree_wizard, https://github.com/think602/spree_wizard
#
# //////////////////////////////////////////////////////
# ================================================
# Controller code preforming Rails SQL lookup
# ------------------------------------------------
module Spree
class WizardsController < Spree::StoreController
# Get taxon and eager load associated objects
# GET /wizards/taxon/1/products.json
def taxon_products
@taxon = Spree::Taxon.
includes(products: { variants: [:images, :default_price, :option_values]}).
includes(products: { master: [:images, :default_price, :option_values]}).
find(params[:taxon_id])
render 'spree/wizards/taxon_products_api', formats: :json, object: @taxon
end
end
end
# ================================================
# RABL JSON rendering code
# ------------------------------------------------
object @taxon
child :products do
attributes :id, :name, :price, :description, :sku, :taxon_ids
node(:attachment_url) { |p| p.images.try(:first).try(:attachment).try(:url) }
node(:announcement ) { false }
child :variants do
attributes :id, :price, :is_master
child :option_values do
attributes :name
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment