sql - How can I order a Rails collection by the attributes of associated records? -
i have 3 models: teachers, students, , assignments
class teacher < activerecord::base has_many :assignments has_many :students, through: assignments, uniq: true end
for given teacher, retrieve list of unique students - simple enough, call:
teacher.students
however, order list of students based on has submitted assignments recently. specifically, student updated assignment appear first, , on.
i stuck on following code, not working:
teacher.students.group("assignments.student_id").order("max(assignments.updated_at) desc")
any suggestions?
it appears original query correct:
teacher.students.group("assignments.student_id").order("max(assignments.updated_at) desc")
i had thought wasn't working because had written bad rspec test failing because wasn't handling timestamps well. once used timecop handle timestamps properly, test passed.
sorry that.
Comments
Post a Comment