Reputation: 5073
A Parent has_many :children. I want to render each Parent's last child, ordered by the child's created_at date. I'm having trouble figuring out how to do this with active record & rails.
Upvotes: 1
Views: 348
Reputation: 1136
Try:
youngest_kids = Parent.includes( :children ).map { |parent| parent.children.last }.compact
compact
removes the nil
s returned for parent
s without children
Upvotes: 2
Reputation: 121
try
Parents.all.each do |parent|
parent.children.order(:created_at).last
end
Upvotes: 0