Reputation: 10214
I have two models: Enrollment and Invoice.
enrollment belongs to invoice
invoice has many enrollments
So I have this query: @enrollments = Enrollment.where('invoice_id IS NOT NULL')
to get all enrollments that belong to an invoice.
But I need to make some type of join because what I really want is the invoices (id, invoice_number and totals) that have enrollments associated.
How can I do that?
What I have tried so far in the console...
enrollments = Enrollment.where('invoice_id IS NOT NULL').joins(:invoice)
enrollments.each do |enrollment|
puts enrollment.invoice_number
end
I get NoMethodError: undefined method invoice_number for #<Enrollment:0x00000006a1e1a8>
because I can only access the id's of the invoices.
Upvotes: 1
Views: 247