0

This query is not working, pease help. I'm trying to include a second and a third deep-level of association.

Pedido > has_one(products_pedido) > has_one(product_size)

@pedidos = Pedido.includes(:pedidos_payments, :products_pedidos => { :product_size } , :estado, :brand, :customer ).where(:is_quote => false)

Ps: I know products_pedido is mispelled according to ActiveRecord good practices :).

1 Answers1

0

Without a stacktrace here's what I suggest:

Assuming your has_one method name is products_pedidos, your issue looks like a problem with your hash syntax.

Your syntax creates a hash with key products_pedidos that returns a hash without a value. This is probably where the error is occurring.

@pedidos = Pedido.includes(:products_pedidos => { :product_size })

What you likely want is this which returns a hash with key products_pedidos with value product_size

@pedidos = Pedido.includes({products_pedidos: :product_size })

The Entire query might look like:

@pedidos = Pedido.includes(
  :pedidos_payments,
  {products_pedidos :product_size},
  :estado,
  :brand,
  :customer
).where(is_quote: false)

Here's a great post explaining a bit more about ActiveRecord nested relationship loading: Rails - Nested includes on Active Records?. I'd also suggest fixing the naming on products_pedido to follow good naming practices.

rosenjd4
  • 1
  • 1