I have an array of Rails ActiveRecord models in which I need to group by name and then sort by topic. For instance the initial array may look like this (hopefully formatting comes out correctly): Name Topic News Politics Sports Soccer Finance Stocks Finance Bonds News World News Entertainment I will need to display the list as follows: Name Topic Finance Bonds Stocks News Entertainment Politics World Sports Soccer As you can see, we only display the name on the first entry of that name. Sure, I can simply sort a one-dimensional array of these objects and place the logic into the presentation such that it checks if the name it is processing now differs from the last name. But that didn't see so clean. Is there a better Ruby way in doing this?
on 2007-04-01 15:50
on 2007-04-01 19:56
Is your Topic not a proper ActiveRecord model? It seems like you have a one-to-many relationship. If you have Topic and, say, SubTopics, you should just traverse the Topics first, then their SubTopics. You can even get all the data in one query with :include.