2010-08-25 44 views
1

我需要根據料品的訂單關係字段(payment_method_meta_type.name)生成需要按欄定購的收據清單。如何按多關係表的字段進行排序?

型號:

Receipt 
Deposit 
PaymentMethodMetaType 

存款型號:

class Deposit < ActiveRecord::Base 

    belongs_to :payment_method_meta_type 
    has_many :receipts, :class_name=>"Receipt", :foreign_key=>"deposit_id", 
        :dependent => :destroy 
end 

我在控制了收益的數組已:

@receipts = Receipt.find(:all, :conditions => ["date BETWEEN ? AND ?", 
    @start_date, @end_date], :order => "date DESC, id DESC", 
    :limit => limit, :offset => offset) 

在視圖我可以顯示payment_method_meta_type。名稱以及

- @receipts.each do |o| 
    %tr. 
    ..... 
    %td #{o.receipt_number} 
    %td #{o.deposit.payment_method_meta_type.name} 
    ..... 

但是,當我創建收據數組的集合時,如何在控制器中按照receipts.deposit.payment_method_meta_type.name的順序顯示列表?

回答

0

THX,我終於工作了,使用 '包括' 在查詢字符串

@receipts = Receipt.find(:all, :include => {:deposit => [:payment_method_meta_type] } ,:conditions => ["Receipts.business_date BETWEEN ? AND ?", @current_month_start_date, @current_month_end_date],:order => "tag_types.name , Receipts.business_date DESC",:limit => limit) 
0

試試這個:

@receipts = Receipt.all(:joins => {:deposit => :payment_method_meta_type}, 
       :conditions => {:date => @[email protected]_date}, 
       :order => "payment_method_meta_types.name ASC", 
       :limit => limit, :offset => offset) 
相關問題