title: Ordering by a related model attribute in Ruby/Sinatra
body: <p>I have simple Booking and Client models:</p>
  
  <pre><code>class Client
    include DataMapper::Resource
    property :id, Serial
    property :name, String, required: true
    property :email, String, required: true
    #....
    has n, :bookings, constraint: :destroy
  end
  
  class Booking  
    include DataMapper::Resource  
    property :id, Serial
    property :start_date, Date
    property :end_date, Date
    #....
    belongs_to :client
  end
  </code></pre>
  
  <p>In the view a table is generated as expected:</p>
  
  <pre><code>&lt;% @bookings.each do |booking| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= booking.client.name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= booking.start_date %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= booking.end_date %&gt;&lt;/td&gt;
      ....
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And I want order the table as by clicking on the respective table column heading.</p>
  
  <p>I can get the table to sort by :start_date, or :end_date, and also by :client_id, but :client_id isn't very useful; I want to be able to sort by the :name attribute of the Client model with :client_id</p>
  
  <p>Could someone tell me what format my controller needs to take, please?</p>
  
  <pre><code>get '/bookings' do
    @bookings = Booking.all(order: :client_id.name) #no method error
    @bookings = Booking.all(order: [:client_id][:name]) #can't convert to integer
    erb :bookings
  end
  </code></pre>
  
  <p>Thanks</p>