Reputation: 47146
I have Models A and B. A has_many B. Now I wanna fetch all the records of A ordered by the B's updated_at. Is it possible to write a find function for this?
Upvotes: 0
Views: 59
Reputation: 43318
Instead of A and B, lets say a Teacher has many Students:
teachers = Teacher.joins(:students).order('students.updated_at').uniq
Upvotes: 1
Reputation: 26
Try something like this:
A.find(:all, :include => ['Bs'], :order => 'Bs.updated_at')
Upvotes: 1
Reputation: 6857
It doesn't makes sense when A has many Bs.
Consider the example:
A1 => [B1, B2]
A2 => [B3, B4]
Assuming the following:
B1.updated_at => 2.days.ago
B2.updated_at => 1.day.ago
B3.updated_at => 4.days.ago
B4.updated_at => 1.days.from_now
Now which one should come first, A1 or A2?
Upvotes: 0