title: Using :includes with variables and further queries
body: <p>Here's a query:</p>
  
  <pre><code>@projects = @user.projects.includes(:company =&gt; :workers)
  </code></pre>
  
  <p>In my view, I want to order a list of these projects by the <a href="https://github.com/mbleigh/acts-as-taggable-on" rel="nofollow">tags</a> that belong to the company.</p>
  
  <p>So, projects belonging to the company's <code>urgent</code> tag come first, then <code>elevated</code>, then all others.</p>
  
  <p>I think one way to do this is to define <code>@companies</code>, then define:</p>
  
  <pre><code>@urgent = @companies.tagged_with('urgent')
  @elevated = @companies.tagged_with('elevated')
  @others = @companies.tagged_with('urgent', 'elevated', :exclude =&gt; true)
  
  # view:
  @urgent.each do |u| ... end
  @elevated.each do |e| ... end
  @others.each do |o| ... end
  </code></pre>
  
  <h3>1. How do I get @companies in the first place?</h3>
  
  <p>How can you collect all the companies together when the first query is the one I showed at the top? <code>@companies = @projects.collect{|p| p.company}</code> produces a non-searchable array.</p>
  
  <h3>2. Is there a more elegant way of displaying the projects than doing three loops?</h3>