title: Complex ordering of Active Record Collection in Rails
body: <p>I have an Active Record collection of games that includes base games and board games.</p>
  
  <pre><code># All Games (Base Games and Expansions)
  games = current_user.games
  </code></pre>
  
  <p>I have a filter method that filters the games based on different parameters. However, I'd like to move the expansions next to the base game in the collection. How would I go about doing that. I can get the base games and expansions by doing:</p>
  
  <pre><code># Base Games
  games.where(game_type: 'boardgame')
  
  # Expansions
  games.where(game_type: 'boardgameexpansion')
  </code></pre>
  
  <p>An expansion can technically have multiple parents, so you can get to the first parent game by doing just that:</p>
  
  <pre><code>my_game.parents.first
  </code></pre>
  
  <p><strong>How can I locate the parent game in the <code>games</code> collection, and move the expansion next to it?</strong> Even though an expansion can have multiple parents, I'll assume the first parent is always okay to move to.</p>