父对象有_个:子对象。我想呈现每个Parent的最后一个子项,按子项的created_at日期排序。我很难弄清楚如何使用活动唱片&轨道。
尝试:
youngest_kids = Parent.includes( :children ).map { |parent| parent.children.last }.compact
compact
删除为没有children
的parent
返回的nil
尝试
Parents.all.each do |parent|
parent.children.order(:created_at).last
end