Ruby on rails 轨道3。获取相关模型的属性

Ruby on rails 轨道3。获取相关模型的属性,ruby-on-rails,ruby,activerecord,join,Ruby On Rails,Ruby,Activerecord,Join,我有两种型号:注册和发票 enrollment belongs to invoice invoice has many enrollments 所以我有这个查询:@enrollments=Enrollment.where('invoice\u id不为NULL')来获取属于发票的所有注册 但我需要进行某种类型的加入,因为我真正想要的是与注册相关联的发票(id、发票号和总数) 我该怎么做 到目前为止,我在控制台中尝试的内容 enrollments = Enrollment.where('invo

我有两种型号:注册和发票

enrollment belongs to invoice
invoice has many enrollments
所以我有这个查询:
@enrollments=Enrollment.where('invoice\u id不为NULL')
来获取属于发票的所有注册

但我需要进行某种类型的加入,因为我真正想要的是与注册相关联的发票(id、发票号和总数)

我该怎么做

到目前为止,我在控制台中尝试的内容

enrollments = Enrollment.where('invoice_id IS NOT NULL').joins(:invoice)
enrollments.each do |enrollment|
  puts enrollment.invoice_number
end
我得到了#的
NoMethodError:undefined方法invoice_number,因为我只能访问发票的id

Invoice.joins(:注册)。uniq
Invoice.joins(:注册)。uniq