1

Here is my scheme:

I have

parcelas belongs_to pagamento
pagamento belongs_to :pagavel, polymorphic: true 
pagavel belongs_to :agencia

Now I want to create a scope in Parcela that would look like

Parcela.from_agencia(agencia_id) and that would query only the Parcelas that belong to pagamento that belong to pagavel that belong to this agencia_id.

Guido
  • 387
  • 2
  • 13

1 Answers1

0

Use scope with joins in parcela.rb:

scope :from_agencia, ->(agencia_id) { joins(pagamento: :pagavel).where(pagavels: {agencia_id: agencia_id}) }

Inpego
  • 2,657
  • 13
  • 14
  • Hey there, thans for answering, I did: Parcela.includes(pagamento: :pagavel).where(pagavels: {agencia_id: 1}), and got Can not eagerly load the polymorphic association :pagavel – Guido Nov 05 '15 at 12:09
  • Missed `polymorphic` in your scheme, if you really need it, use this workaround 
http://stackoverflow.com/questions/16123492/eager-load-polymorphic – Inpego Nov 05 '15 at 16:40