通过Ruby / Sinatra中的相关模型属性进行排序

时间:2013-02-17 12:53:45

标签: ruby sinatra datamapper

我有简单的预订和客户模型:

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

在视图中,按预期生成表:

<% @bookings.each do |booking| %>
  <tr>
    <td><%= booking.client.name %></td>
    <td><%= booking.start_date %></td>
    <td><%= booking.end_date %></td>
    ....
  </tr>
<% end %>

我想通过点击相应的表格列标题来订购表格。

我可以通过以下方式对表进行排序:start_date或:end_date,以及:client_id,但是:client_id不是很有用;我希望能够使用以下客户端模型的:name属性进行排序:client_id

有人能告诉我控制器需要采用什么格式吗?

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

由于

0 个答案:

没有答案