activerecord - Spree Ruby on rails query -
trying learn ruby , spree
i've got query in controller that's running fine:
@products = spree::lineitem.select("spree_line_items.id, spree_variants.sku, spree_products.name, spree_line_items.quantity") .joins(:order).joins(:variant).joins(:product) .where(spree_orders: {state: 'complete', shipment_state: 'ready'})
when run query in view
<% @products.each |lineitem| %> <%= lineitem.id %><br> <% end %>
this works fine , spits out of id's i'm wanting output other bits, sku, name , quantity other tables. how bring them in here?
if reference them same id delegationerror.
to answer query, can do
@products = spree::lineitem.select("spree_line_items.id, spree_variants.sku variant_sku, spree_products.name product_name, spree_line_items.quantity quantity") .joins(:order).joins(:variant).joins(:product) .where(spree_orders: {state: 'complete', shipment_state: 'ready'})
then access sku lineitem.variant_sku
, name lineitem.product_name
, quantity lineitem.quantity
.
but appropriate way achieve is
spree::lineitem.joins(:order).includes(variant: :product) .where(spree_orders: {state: 'complete', shipment_state: 'ready'})
and access attributes lineitem.sku
, lineitem.name
, lineitem.quantity
we can because sku
, name
delegated variant
in line_item
model. same reason getting delegationerror.
Comments
Post a Comment