2012-08-03 77 views
0

我目前正在创建一个应用程序来跟踪我的暗黑破坏神3拍卖,并且我遇到了排序问题。Rails 3根据协会排序复杂

现在我有一个物品页面,其中列出了我最近最近销售的所有物品和信息。每件商品都可以有多个销售,但我只希望最近的销售出现在商品页面上。

我的所有内容都显示正确,并且我可以按项目NAME,GEARSLOT和iLVL排序,因为它们属于Item模型,所以没有任何问题。

但是,按照销售货币或价格排序,证明是棘手的。我已经看了这个,并且发现了很多解决方案,如果每个项目只有一次销售,但没有什么能够帮助我仅通过最近的关联进行排序。

index.html.erb

<table id="item_list"> 
    <tr class="header_row"> 
    <th><%= link_to "Name", :sort => :name %></th> 
    <th><%= link_to "Gearslot", :sort => :gearslot %></th> 
    <th><%= link_to "Ilvl", :sort => :ilvl %></th> 
    <th>Currency</th> 
    <th>Price</th> 
    </tr> 

<% @items.each do |item| %> 
<% last_sale = item.sales.find(:last) %> 
<tr> 
    <td><%= item.name %></td> 
    <td><%= item.Gearslot %></td> 
    <td><%= item.iLvl %></td> 
    <td><%= Sale::CURRENCYCODE.index(last_sale.currency) %></td> 
    <td><%= last_sale.price %></td> 
</tr> 

.... 

items_controller.rb

def index 
    @items = Item.order(params[:sort]) 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @items } 
    end 
end 

我已经可以告诉大家,我可能不会做这种最有效的方式来开始,所以任何的建议是不胜感激!

回答