title: Rails and SQL - Joining by multiple table columns
body: <p>I'm still trying to learn SQL and I could use some help ordering by different attributes. What I'm trying to do is get all <code>products</code> and <code>skus</code> and order them first by the <code>collection.name</code>, then by the <code>sku.name</code>. However, both the collection name and the sku name are in different tables that are associated with the products table by a foreign key.</p>
  
  <p>So it would look something like this</p>
  
  <pre><code>product.id | collection.name | product.name | sku.name
  1          | Apple           | Lateral File | A34
  3          | Beaumont        | Desk         | BT450
  2          | Beaumont        | Hutch        | BT451
  5          | Beaumont        | Drawer       | BT452
  7          | Vista           | File         | V246
  6          | Waterfall       | TV Stand     | WF899
  </code></pre>
  
  <p>Any help is appreciated</p>
  
  <p>Here are my models: </p>
  
  <p><strong>product.rb</strong></p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
    attr_accessible               :name, 
                                  :title,
                                  :features,
                                  :collection_id,
                                  :skus_attributes
  
  
    belongs_to                    :collection
  
    has_many                      :skus, dependent: :destroy
    accepts_nested_attributes_for :skus, reject_if: lambda { |a| a[:title].blank? }, allow_destroy: true
  end
  </code></pre>
  
  <p><strong>collection.rb</strong></p>
  
  <pre><code>class Collection &lt; ActiveRecord::Base
    attr_accessible   :name, 
                      :title,
                      :description
  
    has_many :products
  end
  </code></pre>
  
  <p><strong>sku.rb</strong></p>
  
  <pre><code>class Sku &lt; ActiveRecord::Base
    default_scope order('skus.id ASC')
  
    attr_accessible               :name,
                                  :title,
                                  :product_id
  
    belongs_to                    :product
  
  end
  </code></pre>