我有一些活動,人。他們之間有多對多的關係,所以有一個PersonEvent將Events與People連接起來。請幫我想一想Ruby on Rails中更好的算法
事件有一個日期,然後鍵入
PersonEvent有事項標識和爲person_id
人都有一個名字
我試圖建立一個搜索表單,允許用戶通過搜索類型,然後返回參加過此類事件的人員列表以及他們參加此類事件的最後日期。這應該在控制器中。
我能想到的唯一解決方案涉及嵌套循環,可能會運行得非常慢。我肯定是循環了很多我不需要的東西。
For each person in Person.all
For each personevent in PersonEvent.all
Add the personevent to an array if the person_event.event.type is correct
Now, loop through the array and find the event with the latest date. That's the date of the last Event attendance.
任何人都可以推薦一個更好的算法嗎?