views:

22

answers:

2

Hi , I need to generate a list of receipts in rails which need to be ordered by item's order relationship field (payment_method_meta_type.name).

Models :

Receipt
Deposit
PaymentMethodMetaType

In Deposit Model:

class Deposit < ActiveRecord::Base

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

I got an Array of Receipts in controller already :

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

In the view I can show the payment_method_meta_type.name as well

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

But how can I show the list by the order of receipts.deposit.payment_method_meta_type.name in the controller when I create the collection of the receipts array?

Cheers~

A: 

Try this:

@receipts = Receipt.all(:joins => {:deposit => :payment_method_meta_type},
              :conditions => {:date => @start_date..@end_date}, 
              :order => "payment_method_meta_types.name ASC", 
              :limit => limit, :offset => offset)
KandadaBoggu
A: 

thx , I finally work it out, use 'include' in the query string

@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)
Gz Rain