I've been trying to deal with this problem for a couple of hours now and haven't been able to come up with a clean solution. It seems I'm not too good with rails...

Anyway, I have the following:

![Diagram](https://i3.photobucket.com/albums/y94/Logox/model2-1.png)

In code:

```
class Article < ActiveRecord::Base
  has_many :line_aspects
  has_many :aspects, :through => :line_aspects
  #plus a 'name' field
end

class LineAspect < ActiveRecord::Base
  belongs_to :article
  belongs_to :aspect
  #plus a 'value' field
end

class Aspect < ActiveRecord::Base
  has_many :line_aspects
  has_many :articles, :through => :line_aspects
  #plus a 'name' field
end

```

Now, what I would like to do, is to sort these in two steps. First sort of Articles by their Articles.name, and then inside sort them by Aspect.name (note, not the middleman). 

For instance, alphabetically (sorry if the notation is not correct):

```
[{
   article => 'Apple',
   line_aspects => [
      {:value => 'red'}, #corresponding to the Attribute with :name => 'color'
      {:value => 'small'} #corresponding to the Attribute with :name => 'shape'
   ]
},{
   article => 'Watermelon',
   line_aspects => [
      {:value => 'green'}, #corresponding to the Attribute with :name => 'color'
      {:value => 'big'} #corresponding to the Attribute with :name => 'shape'
   ]
}]

```

Again, note that these are ordered by the aspect name (color before shape) instead of the specific values of each line (red before green). My intention is to display these in a table in the view. 

With this result:

![Table](https://i3.photobucket.com/albums/y94/Logox/Pantallazo.png)

This is the code I'm using:

```
<table>
  <tr>
    <th>Category</th>
    <% @articles.each do |article| -%>
      <th><%= link_to article.name, article -%></th>
    <% end -%>
  </tr>

  <% @aspects.each do |aspect| -%>
    <tr>
      <td><%= aspect.name -%></td>

      <% aspect.line_aspects.each do |line_aspect| -%>
        <td><%= line_aspect.value %></td>
      <% end -%>
    </tr>
  <% end -%>
</table>

```

I have not found a good way to do this in rails yet (without resorting to N queries). Can anyone tell me a good way to do it (even changing the view, if my approach is wrong)?

(I found a [similar question in hyphen](http://www.experts-exchange.com/Programming/Languages/Scripting/Ruby/RubyOnRails/Q_24035892.html))

UPDATE: Here's how I'd do it in SQL:

```
SELECT line_aspects.value FROM line_aspects, aspects, articles 
WHERE articles.id = line_aspects.article_id 
   AND aspects.id = line_aspects.aspect_id
ORDER BY aspects.name, articles.name

```

But I'd like to do it the rails way.

UPDATE: Added the view code. Which might expose my predicament a bit better.