我有两种模式:注册和发票。
enrollment belongs to invoice
invoice has many enrollments
所以我有这个查询:@enrollments = Enrollment.where('invoice_id IS NOT NULL')
来获取属于发票的所有注册。
但是我需要进行某种类型的连接,因为我真正想要的是具有相关注册的发票(id,invoice_number和totals)。
我该怎么做?
到目前为止我在控制台中尝试了什么...
enrollments = Enrollment.where('invoice_id IS NOT NULL').joins(:invoice)
enrollments.each do |enrollment|
puts enrollment.invoice_number
end
我得到NoMethodError: undefined method invoice_number for #<Enrollment:0x00000006a1e1a8>
,因为我只能访问发票的ID。
答案 0 :(得分:4)
Invoice.joins(:enrollments).uniq